chris
chris

Reputation: 21197

How to change .htaccess to redirect all non-www links to www pages?

How can I redirect all non-www links to www links? I have found solutions on the internet, but they only redirect the domain name. How do I make this general: http://example.com/testing should redirect to http://www.example.com/testing?

Upvotes: 1

Views: 2998

Answers (2)

reko_t
reko_t

Reputation: 56430

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.yourdomain.com/$1

If you want something generic that works for any domain, you can try something like:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+)$
RewriteRule ^(.*)$ http://www.%1/$1

Upvotes: 2

Marek Karbarz
Marek Karbarz

Reputation: 29294

try something like this

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

Upvotes: 5

Related Questions