azz0r
azz0r

Reputation: 3311

Reload if no www. is present via HTACCESS

Currently we do it per site in the .htaccess, is there away to regex this piece of code up?

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

RewriteCond %{HTTP_HOST} ^soupandcookies-takeaway.com
RewriteRule (.*) http://www.soupandcookies-takeaway.com/$1 [R=301,L]

As it has been a chore as we are starting to us ALOT of domains on one codebase.

Many thanks

Upvotes: 1

Views: 170

Answers (1)

Linus Kleen
Linus Kleen

Reputation: 34662

For non-HTTPS requests:

RewriteCond %{HTTPS} =off
RewriteCond %{SERVER_NAME} !^www\.
RewriteCond %{SERVER_NAME} ^(.+)$
RewriteRule (.*) http://www.%1/$1 [R=301,L]

And for HTTPS requests:

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

Also note that relying on HTTP_HOST is unsecure. Its value is taken from the HTTP header field Host: ... which can be forged, if your default vhost handles all requests.

Upvotes: 3

Related Questions