简体   繁体   中英

Simplify redirect domain to non-www SSL

I am using the follow .htaccess code to do the following:

.htaccess

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

RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteRule ^([^.]+)/?$ index.php?get=$1 [L]

This works fine but can this code still be simplified?

You can combine http->https rule and www removal rule into a single rule and have your .htaccess as this:

RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ index.php?get=$0 [L,QSA]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM