我正在尝试将所有请求重定向到:
要求1: https协议,特定子文件夹,没有www子域。
因此,例如,“http://www.example.com”重定向到“https://example.com/subfolder/”
要求2: 子文件夹中页面的请求不应重定向到根子文件夹。 例如:“http://www.example.com/subfolder/subfolder2/"redirects to”https://example.com/subfolder/subfolder2/“
要求3: 要求1&除了位于根目录中的robots.txt文件之外的所有请求都应该发生2(因此,以下网址不会重定向):
这是我到目前为止所拥有的:
RewriteEngine on
# anything not a file or a directory goes to subfolder root
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://example.com/subfolder/$1 [R=301]
# non secure http protocol redirects to secure
RewriteCond %{SERVER_PORT} !443
RewriteRule (.*) https://example.com/subfolder [R=301]
# root redirects to subfolder root file
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/subfolder/index.php [R=301]
# www subdomain is redirected to domain
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301]
# Last check to redirect with entire trail, not sure if this ever catches a request.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
然而,example.com / robots.txt文件被重定向到子文件夹。
答案 0 :(得分:1)
以下是我的完整解决方案:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://example.com/subfolder/$1 [R=301]
RewriteCond %{REQUEST_URI} /robots.txt
RewriteRule ^(.*)$ $1 [L]
RewriteRule ^(/)?$ https://example.com/subfolder/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]