Forcefully redirect HTTP to HTTPS with WWW and remove trailing slash(/)

Sharing is caring!

372 Views -

Duplicate content problem caused by multiple redirections
If SSL is installed on your webserver and has not created any rewrite rules, all of these pages probably return a valid 200 page.

http://domian.com/service
http://domian.com/service/
http://www.domian.com/service
http://www.domian.com/service/
https://domian.com/service
https://domian.com/service/
https://www.domian.com/service
https://www.domian.com/service/

The best solution is

#### Forcefully add HTTPS://WWW and remove trailing slash (/) from files ####
## On rewrite engine
RewriteEngine on

# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://www.domian.com/%1 [R=301,L]

# Include trailing slash on directory 
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://www.domian.com/$1/ [R=301,L]

# Forcefully add HTTPS and WWW 
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off  
RewriteRule ^(.*)$ https://www.domian.com/$1 [R=301,L]

 

4 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments