.htaccess重写多级子目录异常
我一直在使用我自己的自定义 CMS 的服务器上使用 .htaccess 脚本来清理 URL:
# 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 应该将它们重定向到 www.mysite.com/main-dir/index.html
如果客户端在 main-dir 中添加更多目录,则方向应该是其中的index.html 例如www.mysite.com/main-dir/some-dir/index.html
所以本质上我需要从常规的pages.php重定向中排除main-dir及其任何子目录,并确保它们都重定向到其中的 index.html
。
我已经尝试了很多方法来完成此任务,包括在子目录中添加仅 RewriteEngine off
的 .htaccess 文件,并添加各种条件和规则,例如:
RewriteCond %{REQUEST_URI} !^/collections/^
并且
RewriteRule ^(collections)/$1 [L]
没有任何效果。
目前,如果网络用户访问 www.mysite.com/main-dir
,他们会被重定向到pages.php 脚本,并且不会被豁免,尽管如果他们访问 www.mysite.com /main-dir/index.html
他们确实到达了 html 页面。
任何帮助将不胜感激。
I've been using an .htaccess script for clean URLS on my servers using my own custom CMS:
# 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]
This works great at redirecting all requests to the pages.php script of my CMS.
The problem is that on rare occasion I have a client that wants to add a sub-directory in which they can add unlimited additional sub-directories all of which need to be redirected to index.html within them. For this discussion lets call this primary sub-directory main-dir which when a user goes to www.mysite.com/main-dir should re-direct them to www.mysite.com/main-dir/index.html
If the client adds more directories in main-dir the direction should be to an index.html within them E.g. www.mysite.com/main-dir/some-dir/index.html
So essentially I need to exempt main-dir and any of it's sub-directories from the regular pages.php redirect and make certain they all redirect to index.html
within them.
I've tried a whole host of things to accomplish this including adding an .htaccess file in the sub-directory with only RewriteEngine off
and adding various conditions and rules such as:
RewriteCond %{REQUEST_URI} !^/collections/^
and
RewriteRule ^(collections)/$1 [L]
Nothing has worked.
currently if a web user goes to www.mysite.com/main-dir
they are re-directed to the pages.php script and not exempted although if they go to www.mysite.com/main-dir/index.html
they do get to the html page.
Any assistance would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在每个 RewriteRule 中使用负前瞻,如下所示:
这将使上述规则适用于除 /main-dir/ 之外的所有内容。
另外,对于在
/main-dir/
内提供index.html
,您不需要执行任何操作,因为 Apache 中默认显示了该内容。You have to just make use of negative lookahead in every RewriteRule like this:
That will make above rules applicable for everything except
/main-dir/
.Also for serving
index.html
inside/main-dir/
you don't need to do anything since that is shown by default in Apache.只需将以下内容添加到 htaccess 的顶部即可。这将防止url被重写为pages.php,url的第一部分是根目录中的现有目录。因此,如果存在名为 xxx 的目录,则 example.com/xxx/whatever/ 将不会被重写。
您的 RewriteCond 看起来不错,但您必须在每个 RewriteRule 前面重复它。其他两个(
... !-d
和... !-f
)也是如此。但我建议您使用下面的方法,这样您就不必一遍又一遍地重复它们。最后你的 htaccess 应该看起来像这样:
Just add the following to the top of the htaccess. This will prevent the url to be rewritten to pages.php is the first part of the url is an existing directory in the root. So example.com/xxx/whatever/ will not be rewritten if directory with name xxx exists.
Your RewriteCond looked okay, but you had to repeat it in front of every RewriteRule. The same goes for the other two (
... !-d
and... !-f
). But I would suggest the way I use below, so you don't have to repeat them over and over again.In the end your htaccess should look something like this: