我有一个Web应用程序与多个页面。 .htaccess文件将domain-name.com/之后的所有内容redirect到我的index.php文件中,该文件处理input并呈现相应的页面。
但是,我也有一些实际的目录下domain-name.com/需要从redirect除外。 例如,我的PHPMyAdmin目录(/ pma)。
一切工作正常,直到我添加行redirect报告/ [名称]索引页也。 现在,我的/ pma不会进入PMA目录,而是进入我的应用程序的index.php。
如果我使用/pma/index.php,它可以工作,但是为什么/ pma会停止使用那个新的重写规则。
注释报告的RewriteRule导致一切再次运作。
我无法弄清楚为什么。 谢谢你的帮助!
.htaccess文件:
RewriteEngine On # Redirect http to https (http://htaccesscheatsheet.com/#force-https) RewriteCond %{HTTPS} !on RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} # Ignore the following directories (don't remap to page engine) RewriteCond %{REQUEST_URI} !^\/(pma*|css) #Redirect all /reports/[report name] requests to index.php?page=reports/[report name] RewriteRule ^reports/([a-z0-9\-_]+)\/{0,1}$ index.php?page=reports/$1 [NC,QSA,L] #Redirect all /[pagename] requests to index.php?page=[pagename]&[querystring] RewriteRule ^([a-z0-9\-_]+)\/{0,1}$ index.php?page=$1 [NC,QSA,L] # Return .json files with the correct mime type (needed to support manifest.json) AddType application/json .json
RewriteCond只适用于下一个RewriteRule 。 你现在有两条规则,每条规则都需要例外。
所以只需重复一下RewriteCond行了
#Redirect all /[pagename] requests (with exceptions) to index.php?page=[pagename]&[querystring] RewriteCond %{REQUEST_URI} !^\/(pma*|css) RewriteRule ^([a-z0-9\-_]+)\/{0,1}$ index.php?page=$1 [NC,QSA,L]
如果你喜欢PCRE正则expression式,可以select另一种方法:
RewriteRule ^(?!pma|css)([a-z0-9\-_]+)\/{0,1}$ index.php?page=$1 [NC,QSA,L]
但是,这有点模糊。 如果你将有几个例外的规则,那么我会接受David的解决scheme更容易阅读。
像这样的例外更容易适用于最终的规则,而不会做任何重写。 将您的RewriteCond行replace为:
RewriteRule ^pma/ - [L] RewriteRule ^css/ - [L]