Gerico
Gerico

Reputation: 5169

How to use Apaches RedirectMatch to force non www version of domain

I've only just discovered Apaches Redirect method as an alternative to using a .htaccess file in order to force a non www version of my domain.

The problem is I need to ensure any inner pages redirect to the non www version properly as at the moment the simple Redirect / http://domainname.co.uk will only effectively deal with my homepage. How can I set up a rule that will deal with any inner pages as well?

Additionally is this better or worse than just using my .htaccess file?

Upvotes: 0

Views: 38

Answers (1)

Welsh
Welsh

Reputation: 5468

The benefit of keeping it out of .htaccess files is it becomes a bit more maintainable and you don't have these files everywhere.

However something like this:

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

Will redirect non www to www for any page and for the reverse (www to non-www) it should be:

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

Upvotes: 1

Related Questions