問題描述
.htaccess https、www 和子域靜默重寫 (.htaccess https, www and subdomain silent rewrite)
我需要:
添加 www,所以用戶將:
- 輸入
https://example.com/test .html
- 在瀏覽器中查看
https://www.example.com/test.html
- 從
example 獲取文件.com/test.html
- 輸入
添加http‑> https 重定向,因此用戶將:
- 輸入
http://www.example.com/test.html
- 在瀏覽器中查看
https://www.example.com/test.html
- 從
example.com/test.html
獲取文件
- 輸入
添加子域 silent 重定向,因此用戶將:<
參考解法
方法 1:
You may try these rules in site root .htaccess:
DirectoryIndex index.php RewriteEngine On # add www for main domain RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L] # add https for all domains RewriteCond %{HTTPS} !on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L] # for new domain silently forward to new/ directory RewriteCond %{HTTP_HOST} ^new\.example\.com [NC] RewriteRule !^new/ new%{REQUEST_URI} [NC,L]
Make sure you don't have any other code except these lines in site root and you must clear browser cache completely.
方法 2:
With your shown attempts/samples, could you please try following. Please make sure to you your htaccess rules in following way and clear your browser cache before testing your URLs.
RewriteEngine ON RewriteCond %{HTTPS} !on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L] RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L] RewriteCond %{HTTP_HOST} ^new\.example\.com [NC] RewriteCond %{REQUEST_FILENAME} !‑f RewriteCond %{REQUEST_FILENAME} !‑d RewriteRule ^(.*)/?$ new/index.php?$1 [QSA,L] RewriteCond %{HTTP_HOST} ^new\.example\.com [NC] RewriteCond %{REQUEST_URI} !^/new [NC] RewriteRule ^(.*)/?$ new/$1 [L] RewriteCond %{REQUEST_FILENAME} !‑f RewriteCond %{REQUEST_FILENAME} !‑d RewriteRule ^(.*)/?$ index.php?$1 [QSA,L]
(by Foreen、anubhava、RavinderSingh13)
參考文件