Regex .htaccess重写多级子目录异常
我一直在使用.htaccess脚本在我的服务器上使用我自己的自定义CMS清理URL:Regex .htaccess重写多级子目录异常,regex,apache,.htaccess,mod-rewrite,multi-level,Regex,Apache,.htaccess,Mod Rewrite,Multi Level,我一直在使用.htaccess脚本在我的服务器上使用我自己的自定义CMS清理URL: # this is the initialization Options +FollowSymLinks RewriteEngine On RewriteBase / # these are the rewrite conditions RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILE
# this is the initialization
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# these are the rewrite conditions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# and finally, the rewrite rules
RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2 [L,QSA]
RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3 [L,QSA]
RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3&data3=$4 [L,QSA]
RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5 [L,QSA]
RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5&data5=$6 [L,QSA]
RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5&data5=$6&data6=$7 [L,QSA]
这非常适合将所有请求重定向到我的CMS的pages.php脚本
问题是,在极少数情况下,我的客户机希望添加一个子目录,在其中可以添加无限多个子目录,所有子目录都需要重定向到其中的index.html。对于本讨论,让我们调用此主子目录main dir,当用户转到该目录时,应将其重新定向到www.mysite.com/main dir/index.html
如果客户端在主目录中添加了更多目录,则方向应该是其中的index.html,例如www.mysite.com/main dir/some dir/index.html
因此,本质上我需要从常规pages.php重定向中豁免main dir及其任何子目录,并确保它们都重定向到其中的index.html
我尝试了很多方法来实现这一点,包括在子目录中添加一个.htaccess文件,只关闭RewriteEngine
,并添加各种条件和规则,例如:
RewriteCond %{REQUEST_URI} !^/collections/^
及
什么都没起作用
目前,如果web用户转到www.mysite.com/main dir
,他们将被重新定向到pages.php脚本,而不是豁免,尽管如果他们转到www.mysite.com/main dir/index.html
,他们确实会进入html页面
如有任何帮助,我们将不胜感激。您只需在每个重写规则中使用负前瞻,如下所示:
# ignore all rules if URI starts with /main-dir/
RewriteRule ^main-dir/ - [L,NC]
RewriteRule ^([\w\s+-]+)/?$ pages.php?which_page=$1 [L,QSA]
RewriteRule ^([\w\s+-]+)/([\w\s+-]+)/?$ pages.php?which_page=$1&data1=$2 [L,QSA]
RewriteRule ^([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/?$ pages.php?which_page=$1&data1=$2&data2=$3 [L,QSA]
RewriteRule ^([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/?$ pages.php?which_page=$1&data1=$2&data2=$3&data3=$4 [L,QSA]
RewriteRule ^([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/?$ pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5 [L,QSA]
RewriteRule ^([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/?$ pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5&data5=$6 [L,QSA]
RewriteRule ^([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/?$ pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5&data5=$6&data6=$7 [L,QSA]
# this is the initialization
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# don't rewrite if any of these conditions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* - [L]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^([^/]+)(/(.*))?$ - [L]
# Rewrite all other matching urls to pages.php
#leave old rewrite rules (without the conditions) here, or use the much sorter version I would suggest.
RewriteRule ^([^/]+)(?:/([^/]+))?(?:/([^/]+))?(?:/([^/]+))?(?:/([^/]+))?(?:/([^/]+))?(?:/([^/]+))?/?$ /pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5&data5=$6&data6=$7 [L,QSA]
#old rules
#RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1 [L,QSA]
#RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2 [L,QSA]
#RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3 [L,QSA]
#RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3&data3=$4 [L,QSA]
#RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5 [L,QSA]
#RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5&data5=$6 [L,QSA]
#RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5&data5=$6&data6=$7 [L,QSA]
这将使上述规则适用于除/main dir/
之外的所有内容
同样,对于
index.html
内部/main dir/
服务,您不需要做任何事情,因为在Apache中默认情况下会显示它。只需将以下内容添加到htaccess的顶部即可。这将防止url被重写为页面。php是url的第一部分,它是根目录中的现有目录。因此,如果存在名为xxx的目录,example.com/xxx/whatever/将不会被重写
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^([^/]+)(/(.*))?$ - [L]
你的重写条件看起来不错,但你必须在每个重写规则面前重复它。其他两个(..-d
和..-f
)也是如此。但我会建议我使用下面的方法,这样你就不必一遍又一遍地重复它们了
最终,您的htaccess应该如下所示:
# ignore all rules if URI starts with /main-dir/
RewriteRule ^main-dir/ - [L,NC]
RewriteRule ^([\w\s+-]+)/?$ pages.php?which_page=$1 [L,QSA]
RewriteRule ^([\w\s+-]+)/([\w\s+-]+)/?$ pages.php?which_page=$1&data1=$2 [L,QSA]
RewriteRule ^([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/?$ pages.php?which_page=$1&data1=$2&data2=$3 [L,QSA]
RewriteRule ^([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/?$ pages.php?which_page=$1&data1=$2&data2=$3&data3=$4 [L,QSA]
RewriteRule ^([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/?$ pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5 [L,QSA]
RewriteRule ^([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/?$ pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5&data5=$6 [L,QSA]
RewriteRule ^([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/?$ pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5&data5=$6&data6=$7 [L,QSA]
# this is the initialization
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# don't rewrite if any of these conditions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* - [L]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^([^/]+)(/(.*))?$ - [L]
# Rewrite all other matching urls to pages.php
#leave old rewrite rules (without the conditions) here, or use the much sorter version I would suggest.
RewriteRule ^([^/]+)(?:/([^/]+))?(?:/([^/]+))?(?:/([^/]+))?(?:/([^/]+))?(?:/([^/]+))?(?:/([^/]+))?/?$ /pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5&data5=$6&data6=$7 [L,QSA]
#old rules
#RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1 [L,QSA]
#RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2 [L,QSA]
#RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3 [L,QSA]
#RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3&data3=$4 [L,QSA]
#RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5 [L,QSA]
#RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5&data5=$6 [L,QSA]
#RewriteRule ^([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/([a-zA-Z0-9\-\_\ \+\-]+)/?$ /pages.php?which_page=$1&data1=$2&data2=$3&data3=$4&data4=$5&data5=$6&data6=$7 [L,QSA]
哇,这看起来真的很有希望..这是整合技巧!当我使用它时,我得到:[Wed Jan 11 14:50:38 2012][alert][client 92.81.327.288]/home/auser/wwwroot/asite.com/.htaccess:RewriteRule:无法编译正则表达式“^([^/]+)(?:/([^/]+)(?:/([^/]+))(?:/([^/]+))(?:/([^/])+)(?:/)/([^/))/)/)/(?:/([^/])/)/)/)/)/(?:/)。\?在您的评论中,最后一个
^
和后面的/
之间可能有一些隐藏字符。这可能会导致问题。我在testserver上测试了该规则,它运行得非常好。