将.htaccess中的mod_rewrite规则转换为httpd.conf

时间:2010-12-01 11:09:49

标签: .htaccess url-rewriting httpd.conf

我有一组html文件:www.example.com和“主站点”(测试版):www.example.com/abcd/(需要这些mod_rewrite规则)

mod_rewrite规则在.htaccess中的目录中运行良好。我需要将它们放在httpd.conf中:

规则在.htaccess中显示如下:

RewriteEngine On
RewriteBase /abcd/

RewriteRule ^.*/codelibrary/(.*)$ codelibrary/$1 [L]
RewriteRule ^.*/in_upload/images/(.*)$ in_upload/images/$1 [L]
...

将以下内容复制到httpd.conf文件中无效:

<VirtualHost *:80>
 ServerAlias   www.example.com
 ServerAdmin   nobody@example.com
 DocumentRoot /var/www/html
 ServerSignature On

 <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /abcd/
    ...

它出错了:

RewriteBase: only valid in per-directory config files

我是否需要修改这样的所有规则?

RewriteRule ^.*/abcd/codelibrary/(.*)$ abcd/codelibrary/$1 [L]
RewriteRule ^.*/abcd/in_upload/images/(.*)$ abcd/in_upload/images/$1 [L]

EDIT1

我尝试转换以下行:

...
RewriteRule (.*)ins.html$ browse.php?type=ins [L]
...
RewriteRule ^([a-zA-Z\-]*)/([a-zA-Z0-9\s]*)/([0-9]*)\.html$ browse.php?type=$1&catId=$3 [L]
RewriteRule ^([a-zA-Z\-]*)/([a-zA-Z0-9\s]*)/([a-zA-Z0-9\s]*)/([0-9]*)/([0-9]*)\.html$ browse.php?type=$1&catId=$4&subCatId=$5 [L]
...

要:

...
RewriteRule ^/abcd/ins.html$ abcd/browse.php?type=ins [L]
...
RewriteRule ^/abcd/([a-zA-Z\-]*)/([a-zA-Z0-9\-\s]*)/([0-9]*)\.html$ abcd/browse.php?type=$1&catId=$3 [L]
RewriteRule ^/abcd/([a-zA-Z\-]*)/([a-zA-Z0-9\-\s]*)/([a-zA-Z0-9\-\s]*)/([0-9]*)/([0-9]*)\.html$ abcd/browse.php?type=$1&catId=$4&subCatId=$5 [L]
...

但我得到的错误如下。

当我尝试访问http://www.example.com/abcd/ins/Sustainability/282.html时,我在日志中收到以下错误:

IPADDRESS - - [DATETIME] [www.example.com/sid#2b3cde3c1be0][rid#2b3cec076e70/initial] (2) init rewrite engine with requested uri /abcd/ins/Sustainability/282.html
IPADDRESS - - [DATETIME] [www.example.com/sid#2b3cde3c1be0][rid#2b3cec076e70/initial] (2) rewrite '/abcd/ins/Sustainability/282.html' -> 'abcd/browse.php?type=ins&catId=282'
IPADDRESS - - [DATETIME] [www.example.com/sid#2b3cde3c1be0][rid#2b3cec076e70/initial] (2) local path result: abcd/browse.php

当我尝试访问http://www.example.com/abcd/ins.html时,我在日志中收到以下错误:

IPADDRESS - - [DATETIME] [www.example.com/sid#2b3cde3c1be0][rid#2b3cec076e70/initial] (2) init rewrite engine with requested uri /abcd/ins.html
IPADDRESS - - [DATETIME] [www.example.com/sid#2b3cde3c1be0][rid#2b3cec076e70/initial] (2) rewrite '/abcd/ins.html' -> 'abcd/browse.php?type=ins'
IPADDRESS - - [DATETIME] [www.example.com/sid#2b3cde3c1be0][rid#2b3cec076e70/initial] (2) local path result: abcd/browse.php

我已将RewriteLogLevel设置为2

我们的mod_rewrite规则的未修改版本位于:https://webmasters.stackexchange.com/questions/4237/url-rewritten-pages-take-much-longer-to-load

1 个答案:

答案 0 :(得分:0)

重写规则的目的是什么(即你想要实现的目标)?

通过.htaccess中的原始外观,它正在重定向: www.example.com/abcd/anything/codelibrary/aaaa 至 www.example.com/abcd/codelibrary/aaaa

同样适用于in_upload / images,这是正确的吗?

如果是这样,那么这应该有效:

RewriteRule ^/abcd/.*/codelibrary/(.*)$ /abcd/codelibrary/$1 [L]
RewriteRule ^/abcd/.*/in_upload/images/(.*)$ /abcd/in_upload/images/$1 [L]
相关问题