.htaccess RewriteRule,重定向到https

时间:2016-09-30 11:34:19

标签: .htaccess redirect mod-rewrite

我想将网址重定向到特定格式。例如

abcd.com - > https://www.abcd.com

www.abcd.com - > https://www.abcd.com

http://www.abcd.com - > https://www.abcd.com

loalhost / def - > https://localhost/def

192.168.x.10 / def - > https://192.168.x.10/def

如何编写一个简单的RewriteCond和规则来涵盖上述所有规则?

3 个答案:

答案 0 :(得分:0)

在.htaccess

RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

答案 1 :(得分:0)

您可以在站点根目录中使用这些重定向规则.htaccess:

RewriteEngine On

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

# for rest
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

答案 2 :(得分:0)

试试这个,

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www
RewriteRule https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]

RewriteCond %{HTTPS} off
RewriteRule https://%{HTTP_HOST}%{REQUEST_URI} [R=301]