.htaccess 设置自定义DocumentRoot时,将子文件夹中所有存在和不存在的页面和文件夹重定向到一个页面

q35jwt9p  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(107)

我有下一个文件结构:

.htaccess
index.php
public/
├─ v1/
│  ├─ .htaccess
│  ├─ index.php
├─ index.php

我已经在服务器级别上将文档根设置为public子文件夹,以便将其从url中删除。
我需要所有的请求包含v1的url重定向到v1/index.php

我的根.htaccess

RewriteEngine On
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

v1/.htaccess

RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d  [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ index.php [QSA,L]

预期行为:

不存在的https://example.com/v1/test页面应重定向到https://example.com/v1/
"我得到的是“

Not Found
The requested URL was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

我很确定问题出在第二个.htaccess。我也怀疑自定义文档根在这里也可能是一个问题。

mwg9r5ms

mwg9r5ms1#

试着用下面的方式设置你的root .htaccess一次,试着删除你的内部.htaccess(在你的本地系统中保留它的备份,但不要在系统中)。在测试你的URL之前清除你的浏览器缓存。

RewriteEngine ON

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} !\s/public(?:/\S*)*\s [NC]
RewriteCond %{THE_REQUEST} !\s/v1(?:/\S*)*\s [NC]
RewriteRule ^(.*)/?$ /public/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} !\s/v1(?:/\S*)*\s [NC]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{THE_REQUEST} !\s/v1/?\s [NC]
RewriteRule ^(v1)/.+/?$ /$1/? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^v1/?$  public/v1/index.php [QSA,L]

注意:我会在早上补充详细的解释,因为晚上对我来说太晚了。

相关问题