I always see that most of the web developers are stuck with URL redirection. Like redirect Non-WWW to WWW, Non-WWW to HTTPS and WWW to Non-WWW, WWW to HTTPS using .htaccess file.
So, here I listed all the code of URL redirection using htaccess. Hope it helps you.
So, here I listed all the code of URL redirection using htaccess. Hope it helps you.
Non-WWW and WWW to https://www.example.com/
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
Non-WWW and WWW to https://example.com/
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTPS} off
RewriteCond %{ENV:HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
Non-WWW and /index.php to WWW Redirect
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^(.*)index\.(html|php)$ http://%{HTTP_HOST}/$1 [R=301,L]
WWW to Non-WWW Redirect
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Comments
Post a Comment