这个htaccess出了什么问题? 我正在尝试重定向带有问号的所有内容 “www.mysite.com/?bla=bla” 至 “www.mysite.com/router.php?bla=bla
AddDefaultCharset UTF-8
RewriteEngine On
RewriteRule ^([\w]{1,7})$ short.php?p=$1 [L]
RewriteRule ^([\w]+)\.html$ html/$1.html [L]
RewriteRule ^([\w]+)\.php$ php/$1.php [L]
RewriteRule ^\?(.+)$ router.php?$1 [L]
除了最后一条规则之外,每条规则都有效,它会返回一个:
Forbidden
You don't have permission to access / on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
答案 0 :(得分:2)
您无法访问RewriteRule
内的查询字符串。相反,请使用RewriteCond
来匹配它。在你拥有的位置使用?
,它将它视为正则表达式的特殊字符。
改为使用:
# Prevent redirect loop
# Place above all other rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Non-empty querystring goes to router
RewriteCond %{QUERY_STRING} ^(.+)
# Rewrite to router appending existing querystring (QSA)
RewriteRule ^/?$ router.php [L,QSA]
阻止路由器进入php目录:
RewriteCond %{REQUEST_FILENAME} !^router\.php
RewriteRule ^([\w]+)\.php$ php/$1.php [L]