如果文件夹/文件不存在,我想静默地将域的所有请求重定向到子文件夹。
不幸的是,某些方案无法正常工作或无法正常工作。
有效:http://mydomain.com/index.html - 它显示了production / index.html的内容
不起作用:http://mydomain.com/ - 它不显示production / index.html
有效:http://mydomain.com/test/ - 显示生产/ test / index.html的内容
不起作用:http://mydomain.com/test - 它将网址(而不是静默重定向)重定向到http://mydomain.com/production/test/并显示生产/ test / index.html的内容
以下是.htaccess文件:
Options -Indexes +SymLinksIfOwnerMatch
DirectoryIndex index.php index.html
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/production/
# Don't apply to URLs that go to existing files or folders
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all those to insert /production folder
RewriteRule ^(.*)$ /production/$1 [L]
答案 0 :(得分:1)
这是因为mod_dir
在mod_rewrite
执行后附加了一个尾部斜杠。将规则更改为此可以解决此问题:
DirectorySlash Off
Options -Indexes +SymLinksIfOwnerMatch
DirectoryIndex index.php index.html
RewriteEngine on
RewriteBase /
# add a trailing slash for directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{THE_REQUEST} \s/+([^.]*?[^/])[?\s]
RewriteRule [^/]$ /%1/ [L,R=302,NE]
# take care of landing page:
RewriteRule ^/?$ production/ [L]
# Don't apply to URLs that go to existing files or folders
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all those to insert /production folder
RewriteRule ^((?!production).+)$ production/$1 [L,NC]