user2825802
user2825802

Reputation: 11

htaccess redirction from www to non-www url with ssl certification

i would like to redirect my site form www to non-www url with ssl certificate. i have write the code

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

that works but if url have any get request it'll get redirect to the home page.

Upvotes: 1

Views: 77

Answers (2)

Dhaval Bharadva
Dhaval Bharadva

Reputation: 3083

Try this:

Redirect www urls to non-www

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

OR Alternative code that you can use:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule ^(.*) https://example.com/$1 [R=301,L]

Reference site

Upvotes: 0

arkascha
arkascha

Reputation: 42895

Your $1 does not resolve to any value until you caputure some value in your RewiteRule:

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

PS: I made some additional minor modifications.

Upvotes: 1

Related Questions