views:

485

answers:

3

After a site redesign, I've got a couple of pages that need to be redirected. Everything is staying on the same domain, just a couple of things have been reorganised and/or renamed. They are of the form:

/contact.php

is now:

/contact-us.php

Using the .htaccess file, I've added this line, which is the one I find recommended most:

RedirectMatch 301 /contact.php /contact-us.php

This is mostly fine - it does the job - the problem is, it also redirects:

  • /team1/contact.php
  • /non-existant-folder/contact.php

Is there a way of specifying that I only want to redirect the contact.php in the root?

+1  A: 

This should do it

RedirectPermanent /contact.php /contact-us.php
duckyflip
I can’t see why this should solve the problem. `RedirectPermanent …` is the same as `Redirect 301 …`.
Gumbo
Gumbo, RedirectPermanent takes (URL-Path) as parameter and RedirectMatch takes (Regex URL), Therefore when you are using RedirectMatch without (^) to segnify a begining of line it does not work. Using RedirectPermanent is also much faster then the regex alternatives.
duckyflip
@duckyflip: Ah sorry, you’re right. Misread the `RedirectMatch` with just `Redirect`. But `Redirect` does only a prefix check.
Gumbo
A: 

You could also use a RewriteRule if you wanted the ability to template match and redirect urls.

Nat Ryall
Why was this voted down?
Nat Ryall
+3  A: 

RedirectMatch uses a regular expression that is matched against the URL path. And your regular expression /contact.php just means any URL path that contains /contact.php but not just any URL path that is exactly /contact.php. So use the anchors for the start and end of the string (^ and $):

RedirectMatch 301 ^/contact\.php$ /contact-us.php
Gumbo
Thanks, works like a charm.
Dan