.htaccess https、www和子域静默重写



我需要:

  • 添加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重定向,因此用户将:

    • 类型https://new.example.com/test.html
    • 请参阅浏览器中的https://new.example.com/test.html
    • example.com/new/test.html获取文件
  • 当然是组合的,例如:

    • 类型http://new.example.com/test.html
    • 请参阅浏览器中的https://new.example.com/test.html
    • example.com/new/test.html获取文件

这可能通过.htaccess文件实现吗?最终如何实现?

www和https转发对我来说已经很好了,但我不知道,如何用子域做静默转发:

RewriteEngine On
# https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# WWW
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} !^new. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

谢谢。

您可以在站点根目录中尝试这些规则。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]

请确保除了站点根目录中的这些行之外,您没有任何其他代码,并且必须完全清除浏览器缓存。

对于您显示的尝试/样本,您可以尝试以下操作吗。请确保您的htaccess规则按照以下方式,并在测试您的URL之前清除浏览器缓存。

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]

最新更新