.htaccess 301 redirect of single page

111,317

Solution 1

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

Solution 2

This should do it

RedirectPermanent /contact.php /contact-us.php 

Solution 3

redirect 301 /contact.php /contact-us.php

There is no point using the redirectmatch rule and then have to write your links so they are exact match. If you don't include you don't have to exclude! Just use redirect without match and then use links normally

Share:
111,317
Andras Dosztal
Author by

Andras Dosztal

Updated on July 09, 2022

Comments

  • Andras Dosztal
    Andras Dosztal almost 2 years

    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?