【问题标题】:Converting mod_rewrite rules in .htaccess to httpd.conf将 .htaccess 中的 mod_rewrite 规则转换为 httpd.conf
【发布时间】:2010-12-01 11:09:49
【问题描述】:

我有一组 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]

编辑1

我尝试将以下行从:

...
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

【问题讨论】:

    标签: .htaccess url-rewriting httpd.conf


    【解决方案1】:

    重写规则的目的是什么(即你想达到什么目的)?

    从 .htaccess 中的原始外观来看,它正在重定向: www.example.com/abcd/任何东西/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]
    

    【讨论】:

    • 嗨@Ben - 你能看一下编辑吗 - 我认为更改无法正常工作
    • 那些似乎不是错误,看起来它成功地将它们传递给 abcd/browse.php,浏览器端发生了什么?
    • 嗨@Ben - 它给出了“错误请求您的浏览器发送了一个此服务器无法理解的请求。”对于 Edit1 中提到的两个 URL
    • 嗨@Ben - 你提出的解决方案有一个错误 - 它应该像:RewriteRule ^/abcd/.*/codelibrary/(.*)$ /abcd/codelibrary/$1 [L] [注意/之前 abcd/]你能修改答案以便我接受吗?
    猜你喜欢
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2017-12-27
    • 2015-02-17
    • 1970-01-01
    相关资源
    最近更新 更多