Php htacess,{0,10}工程和#x2B;不';t表示重定向,给出错误500

Php htacess,{0,10}工程和#x2B;不';t表示重定向,给出错误500,php,regex,.htaccess,mod-rewrite,url-rewriting,Php,Regex,.htaccess,Mod Rewrite,Url Rewriting,我希望使用子目录中的文件来处理任何请求。例如: 浏览器请求localhost/style.css 服务器从localhost/static/style.css响应(不重定向浏览器!) 使用以下.htacess文件可以完美地工作: RewriteEngine on RewriteRule ^(.{0,10})$ static/$1 但当我这么做的时候 RewriteEngine on RewriteRule ^(.+)$ static/$1 它因“内部服务器错误”而失败 请求超出了10个内部重

我希望使用子目录中的文件来处理任何请求。例如:

浏览器请求localhost/style.css

服务器从localhost/static/style.css响应(不重定向浏览器!)

使用以下.htacess文件可以完美地工作:

RewriteEngine on
RewriteRule ^(.{0,10})$ static/$1
但当我这么做的时候

RewriteEngine on
RewriteRule ^(.+)$ static/$1

它因“内部服务器错误”而失败

请求超出了10个内部重定向的限制

我猜它是从
style.css
重定向到
static/style.css
,然后再重定向到
static/static/style.css
,依此类推。解决办法是使用:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/static/
RewriteRule ^(.+)$ static/$1
当url以static开头时,RewriteCond行阻止下一个RewriteRule重定向/

这也很有帮助:

htacess在重定向后再次迭代规则,因此它会被递归应用,[L]无法停止


另一个解决办法是

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI} -f
RewriteRule ^(.+)$ static/$1
它仅在重定向后的url与存在的文件匹配时重定向