[英]What is the error in this .htaccess?
Using mod_rewrite, I want to look for all requested files in an assets
directory, and otherwise fallback to index.php
for API calls. 我想使用mod_rewrite在
assets
目录中查找所有请求的文件,否则回index.php
进行API调用。 This is my .htaccess
. 这是我的
.htaccess
。
RewriteEngine on
RewriteRule ^.+\.[^/]+$ assets/$1 [L]
RewriteRule .* index.php
Asset files are determined by whether the last last url segment has a file extension, which means a dot without following slashes. 资产文件由最后一个url段是否具有文件扩展名(即没有后跟斜杠的点)决定。 I already tested the regex at regexpal and it seems to work fine.
我已经在regexpal上测试过regex,它似乎工作正常。
Unfortunately, all request result in a 500 Internal Server Error
using these rewrite rules. 不幸的是,使用这些重写规则,所有请求都会导致
500 Internal Server Error
。 I know that mod_rewrite is set up correctly, since omitting the second line works as expected. 我知道mod_rewrite设置正确,因为省略第二行按预期工作。
You need to add a condition to prevent the rewrite engine from looping: 您需要添加条件以防止重写引擎循环:
RewriteEngine on
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^.+\.[^/]+$ assets/$1 [L]
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule .* index.php
The rewrite engine loops until the URI stops changing, and since .*
matches index.php
as well, it'll continue to loop through that rule until the internal recursion limit is reached you get a 500 error. 重写引擎将循环直到URI停止更改为止,并且由于
.*
与index.php
匹配,因此它将继续循环执行该规则,直到达到内部递归限制为止,然后您将得到500错误。
Additionally, you can add conditions that won't reroute existing files or directories: 此外,您可以添加不会重新路由现有文件或目录的条件:
RewriteEngine on
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^.+\.[^/]+$ assets/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php
Try these rules: 尝试以下规则:
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.