Search code examples
http-redirecthttp-status-code-301

how is the redirect rule from https://www.domain to http://domain?


this is http to https
non www to www

#http -> https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#add www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I dont know reverse code ^^;;
https to http
www to non www

how is the rule in htaccess? thanks


Solution

  • To redirect https to http and www to non-www you can modify your rules like the following :

    RewriteEngine on
    #https -> http
    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP:X-Forwarded-Proto} !http$
    RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    #add non-www.
    RewriteCond %{HTTP_HOST} ^www\.(.+)  [NC]
    RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [L,R=301]