[英]What is the error in this .htaccess?
我想使用mod_rewrite在assets
目录中查找所有请求的文件,否则回index.php
进行API调用。 这是我的.htaccess
。
RewriteEngine on
RewriteRule ^.+\.[^/]+$ assets/$1 [L]
RewriteRule .* index.php
资产文件由最后一个url段是否具有文件扩展名(即没有后跟斜杠的点)决定。 我已经在regexpal上测试过regex,它似乎工作正常。
不幸的是,使用这些重写规则,所有请求都会导致500 Internal Server Error
。 我知道mod_rewrite设置正确,因为省略第二行按预期工作。
您需要添加条件以防止重写引擎循环:
RewriteEngine on
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^.+\.[^/]+$ assets/$1 [L]
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule .* index.php
重写引擎将循环直到URI停止更改为止,并且由于.*
与index.php
匹配,因此它将继续循环执行该规则,直到达到内部递归限制为止,然后您将得到500错误。
此外,您可以添加不会重新路由现有文件或目录的条件:
RewriteEngine on
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^.+\.[^/]+$ assets/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php
尝试以下规则:
RewriteEngine on
# rewrite to assets/file if file exists in assets dir
RewriteCond %{DOCUMENT_ROOT}/assets/$1 -f
RewriteRule ^(.+?\.[^/]+)/?$ assets/$1 [L]
# otherwise rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^assets/ index.php [NC,L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.