我现在正在做的事情
我正在为一个客户开发一个后端。
之前,他将网站数据与下载的文件混合在一起,供朋友或其他目的下载。
示例:
/public
/somedir
somesubdirfile
anotherfile1
anotherfile2
foobar.html
index.html
现在,我实现了通用重写规则,将所有请求代理到网站的新index.php
。
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine "On"
RewriteCond "%{REQUEST_FILENAME}" ".htaccess"
RewriteCond "%{REQUEST_FILENAME}" ".htpasswd"
RewriteCond "%{REQUEST_FILENAME}" "!-s"
RewriteCond "%{REQUEST_FILENAME}" "!-l"
RewriteCond "%{REQUEST_FILENAME}" "!-d"
RewriteRule "^.*$" "public/index.php" [NC,L]
</IfModule>
很好,行得通。
现在我把所有的杂物都移到了一个单独的文件夹中。
/resources
/somedir
somesubdirfile
anotherfile1
anotherfile2
foobar.html
/public
index.php
我需要满足的需求
..。就是改变重写规则。
如果请求与/public
中的特定文件或符号链接不匹配,则必须测试它是否与/resources
中的特定文件或符号链接匹配。如果匹配,则必须发送301 Moved Permanently
,并将请求重定向到/resources
。如果没有匹配,则必须重定向到public/index.php
。
但我真的坚持理解如何像我所要求的那样在复杂的情况下编写适当的重写规则。
我需要一些帮助,谢谢。
发布于 2020-11-05 06:17:11
您可以在站点根.htaccess中尝试以下规则:
RewriteEngine On
# if it exists in /resources then redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/resources/$0 -f
RewriteRule .+ /resources/$0 [L,NE,R=301]
# else route to public/index.php
RewriteCond %{REQUEST_URI} \.(htaccess|htpasswd) [NC]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ public/index.php [L]
https://stackoverflow.com/questions/64689043
复制