所有子目录都使用root index.php文件

时间:2014-09-24 19:08:31

标签: apache .htaccess redirect

我现有的.htaccess文件如下所示:

Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^(.+/)?posts/(.*)/(.*).html$ index.php?content=blog&postid=$2&kwd=$3
RewriteRule ^(.+/)?aposts/(.*)/(.*).html$ index.php?content=autopost&postid=$2&kwd=$3
RewriteRule ^(.+/)?category/(.*)/(.*).html$ index.php?content=category&cat=$2&otext=$3
RewriteRule ^(.+/)?content/(.*).html$ index.php?content=$2
RewriteRule ^(.+/)?searching/(.*).html$ index.php?content=search&k=$2
RewriteRule ^(.+/)?related/(.*).html$ index.php?content=related&k=$2
RewriteRule ^(.+/)?blog/(.*)/(.*).html$ index.php?content=blog&postid=$2&kwd=$3
RewriteRule ^(.+/)?posts$ index.php?content=blog
RewriteRule ^(.+/)?sitemap\.xml$ sitemap.php [L]
RewriteRule ^(.+/)?robots\.txt$ robots.php [L]
ErrorDocument 404 /404.php

我想为所有子目录添加使用root index.php文件的功能。例如,如果访问者转到根目录:

example.com

他们获得根index.php文件。

我想要它,以便任何人去任何其他子目录,例如:

example.com/sub1
example.com/sub1/sub2/sub3/etc..

他们从根目录获得index.php文件但浏览器地址栏仍显示子目录,例如:

example.com/sub1
example.com/sub1/sub2/sub3/etc..

展示如何为此规则添加例外也很棒。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

RewriteBase /下方添加:

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond $1 !^index\.php
RewriteRule ^(.+)$ /index.php [L]

要添加例外,只需添加更多RewriteCond行。例如,要使其/foo/不会被路由到index.php,请添加:

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond $1 !^foo/
RewriteCond $1 !^index\.php
RewriteRule ^(.+)$ /index.php [L]
相关问题