我有一个网站example.com和一个子域,我希望进行以下重定向:
subdomain.example.com-> www.subdomain.example.com
example.com-> www.example.com
我使用以下代码
RewriteEngine On
# Force www. always and SSL
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301, L]
# Force SSL if already www.
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301, L]
现在的问题是 subdomain.example.com 不重定向,但 subdomain.example.com / 可以。
>虽然转到www.example.com,但也会收到服务器错误消息。正确。
使用这些重定向的目的是,我可以在根文件夹中的同一.htaccess文件中,希望将子域更轻松地重定向到subdomain.com,因为我在一台服务器上有多个域。
答案 0 :(得分:0)
我认为您可以像这样将指令组合在一起:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
=>检查HTTPS是否关闭,或者域名不是以www开头,然后重定向到https://www.whatever。