.htaccess 301 redirect of single page

Viewed 113948

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?

7 Answers

It will redirect your store page to your contact page

    <IfModule mod_rewrite.c>
    RewriteEngine On 
    RewriteBase /
    Redirect 301 /storepage /contactpage
    </IfModule>

In case you need to create a dynamic rewrite for all of your URLs. For example, from .php extension to .html with a 301 rewrite, you can use this:

RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^                                                 /%1.html  [L,R=301]
RewriteRule ^(.*).html$                                       $1.php [QSA]
Related