如何使mod_rewrite抑制处理更多规则?

时间:2008-11-12 21:05:39

标签: apache .htaccess mod-rewrite

鉴于我当前的.htaccess文件,如何修改它以检查其他URL路径,例如'/ src / pub / '并将其重写为'/ '而不影响当前的重写?

这是原始的.htaccess文件:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

这是我最近的尝试(不起作用):

RewriteEngine on

RewriteRule ^/src/pub/(.*)$ /$1 [R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

编辑:以下是我想要完成的一些示例:

新附加规则:

From:  http://www.mysite.com/src/pub/validfile.php
To:    http://www.mysite.com/validfile.php

From:  http://www.mysite.com/src/pub/user/detail/testuser
To:    http://www.mysite.com/user/detail/testuser

现有规则(已经工作):

From:  http://www.mysite.com/user/detail/testuser
To:    http://www.mysite.com/index.php?route=user/detail/testuser

2 个答案:

答案 0 :(得分:5)

我猜测问题是第一条规则正在重写URL,然后第二条规则再次重写。

解决方案是将“last”标志添加到第一个规则,如下所示:

RewriteRule ^/src/pub/(.*)$ /$1 [R,L]

答案 1 :(得分:3)

<。>在.htaccess文件中,请改用:

RewriteRule ^src/pub/(.*)$ /$1 [R]

如果您希望处理进一步的规则停止,那么领先的“/”将不会在.htaccess中匹配,只能在httpd.confsrc - 页面底部)内匹配,然后添加L标志:

RewriteRule ^src/pub/(.*)$ /$1 [L,R]

重写日志比较(.htaccess context):

// using ^/src/pub/(.*)$ - leading slash will not work in .htaccess context!
(1) pass through /home/test/src

// using ^src/pub/(.*)$
(2) rewrite 'src/pub/testme' -> '/testme'
(2) explicitly forcing redirect with http://test/testme
(1) escaping http://test/testme for redirect
(1) redirect to http://test/testme [REDIRECT/302]