3

The company I do SEO for have changed their domain name.

I have written 301 rewrites to redirect traffic to the new domain. So far, I have a rewrite for 3 changes, of which there are many. By the time I have finished doing them all, there will be around 30 rewrites, which seems a bit silly!

Is there a wildcard I can use to just make sure the following 3 arguments are met?

  1. Must always point to the www. version.
  2. Must always point to the co.uk version.
  3. Must ALWAYS change old URL to new.

It would be something like this...

(.*)example-old(.*) = www.example-new.co.uk

and

(.*)example-old(.*)/(.*) = www.example-new.co.uk/directory

The below code sorts out problem 3, but it won't solve them all.

If there is a short bit of code I can use, which uses wildcards, this will be perfect.

Any help, much appreciated!


# 301 --- http://example.co.uk => http://www.example-new.co.uk
RewriteCond %{HTTP_HOST} ^example\.co\.uk$
RewriteRule ^$ http://www.example-new.co.uk/? [L,R=301]

# 301 --- http://www.example.co.uk => http://www.example-new.co.uk
RewriteCond %{HTTP_HOST} ^www\.example\.co\.uk$
RewriteRule ^$ http://www.example-new.co.uk/? [L,R=301]

# 301 --- http://example.com => http://www.example-new.co.uk
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^$ http://www.example-new.co.uk/? [L,R=301]

# 301 --- http://www.example.com => http://www.example-new.co.uk
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^$ http://www.example-new.co.uk/? [L,R=301]
Electron
  • 969
  • 1
  • 8
  • 22
  • Using the rewrite engine is a ridiculously overpowered way to do this (and it's harder). See the answer to the following question: http://stackoverflow.com/questions/1100343/apache-redirect-from-non-www-to-www/ – Greg Hewgill Aug 26 '13 at 00:26
  • 1
    Thanks, that looks good. But where does it go? And how do I get it to address the .com vs .co.uk proplem? – Electron Aug 26 '13 at 01:23
  • That config option goes in `httpd.conf`. You can use it to redirect any domain to any other, it's not limited to www vs non-www. – Greg Hewgill Aug 26 '13 at 01:25
  • 1
    I'm very new to Apache. How do I find httpd.conf and edit it? – Electron Aug 26 '13 at 01:50

1 Answers1

11

Assuming you have NO other sub domains but www, you could have this simple rule on your .htaccess file on the root of the domains in question if not the same root of example-new.co.uk:

RewriteEngine On
# anything that is not equal to www.example-new.co.uk
RewriteCond %{HTTP_HOST} !^www\.example-new\.co\.uk$
# redirects to http://www.example-new.co.uk/anything
RewriteRule ^/?(.*)$ http://www.example-new.co.uk/$1 [R=301,L]

It will redirect anything not www.example-new.co.uk with the URL path to the new domain.

So, if I access:

http://example-new.co.uk/new-league <<< no wwww

I will be redirected to:

http://www.example-new.co.uk/new-league

And if I access:

http://www.example-new.com/new-league <<< ends with .com
http://www.example-old.co.uk/new-league <<< domain is different
http://example-old.co.uk/new-league <<< domain is different

So all the 3 above will also redirect to:

http://www.example-new.co.uk/new-league
Prix
  • 19,417
  • 15
  • 73
  • 132
  • Works a treat. Not sure what advantage httpd.conf would have over this, as suggested by Greg. I guess now, my next step is working out how to redirect all 404's to a different page? – Electron Aug 26 '13 at 02:05
  • @msfirth [kindly check this answer as the right answer if it solves your issue, see here how.](http://stackoverflow.com/about) – Prix Aug 26 '13 at 02:06
  • @msfirth well for the 404 depends on what you want to do, do u want them redirect to a custom 404 folder or what? – Prix Aug 26 '13 at 02:07
  • For SEO purposes, making a 404 a rewrite to say a news page, stops you getting bad marks from Google. So 404 to /news would be the ideal solution! – Electron Aug 26 '13 at 02:09
  • @msfirth see my update above, if the folder or file pointed does not exist it redirects to `/news` however I don't think Google likes that all 404's goes to the same page on a 301, and no 404 alarms. – Prix Aug 26 '13 at 02:10
  • The redirect to news didn't work. The whole site went in to a loop no matter which link you followed. I think I will leave this for another day. Its 3:30am. The 404 redirect was a tip from a major SEO company on the internet. I shall look into it further. Don't want to be using any black hat techniques. Many thanks for your help. – Electron Aug 26 '13 at 02:27
  • @msfirth yes, well if you have wordpress or similar CMS you might need to work it differently because WordPress itself have a rule to redirect everything to its index.php and handle everything there so to every it's own. Make a new question and try to describe what you have and how you would like it handled or perhaps even using a wordpress plugin that allows you to redirect the 404's would be easier than a rewrite rule. – Prix Aug 26 '13 at 02:43