问题描述
我有一个网站有两个 .htaccess 文件,一个在项目根目录中,一个在 /Project Root/Public/ 目录中,我想将直接请求重写为“http://localhost/Project Root/index.PHP " 到 "http://localhost/Project Root/" 我该怎么做?
我当前的 .htaccess(s):
项目根目录.htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
/项目根目录/公共/.htaccess
RewriteEngine On
RewriteCond %{THE_REQUEST} \s(?:/+(.+/))?public/ [NC]
RewriteRule ^ /%1 [L,R=301,NE]
DirectoryIndex index.PHP
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.PHP(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.PHP [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 307 ^/$ /index.PHP/
</IfModule>
</IfModule>
解决方法
让你的 Project Root .htaccess
像这样:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+([^/]+/)?index\.php[?\s] [NC]
RewriteRule ^ /%1 [L,R=301,NC,NE]
RewriteRule .* public/$0 [L]
确保在测试前清除浏览器缓存。