簡體   English   中英

將所有非 www 重定向到 www,將所有 http 重定向到 https,並排除子域

[英]redirect all non-www to www, all http to https, and exclude subdomains

我正在嘗試重定向

example.com 到www.example.comhttp://example.come to https://www.example.com

但是,如果用戶輸入

admin.example.com, http://admin.example.com or https://admin.example.com不應重定向到www.admin.example.com

我可以為此使用什么 .htaccess?

您可以在站點根目錄 .htaccess 中使用此規則:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+\.[^.]+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

正則表達式(?:www\\.)?([^.]+\\.[^.]+)將匹配example.comwww.example.com但不匹配admin.example.com

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM