URL rewriting and double slash after domain : how to make it single slash?

Viewed 614

I want to write a rule to redirect (301) all pages from https://dev.example.com to https://www.example.com

On the root of dev.domain.com, on my .htaccess I have put this single line :

RewriteRule ^/(.*)$ https://www.example.com/$1 [R=301,L]

The redirection works but all subpages have a double slash.

Example : https://dev.example.com/contact.html is redirected to https://www.example.com//contact.html

Which is not working ! How can I solve this issue ?

1 Answers

Could you please try following, written and tested with your shown samples. Please clear your browser cache before testing your URLs. Also these Rules will look for URLs which are not having www in their HTTP_HOST variable part and then will remove domain name(very first part till first occurrence of .) with www and rest of the domain name's part.

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?!www\.)(?:[^.]*\.)(.*)
RewriteRule ^(.*)$ https://www.%1%{REQUEST_URI} [NE,L]
Related