Dan
Dan

Reputation: 946

.htaccess 301 redirect of single page

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:

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

Upvotes: 61

Views: 124195

Answers (8)

manoj
manoj

Reputation: 187

Redirect 301 /our-blog/ https://example.com/blog/

Avoid unnecessary redirects; if you are already using trailing slashes in your URLs, include the trailing slash in the both old and new url

Upvotes: 0

chispitaos
chispitaos

Reputation: 937

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]

Upvotes: 0

gurcharan
gurcharan

Reputation: 23

It will redirect your store page to your contact page

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

Upvotes: 0

Jon
Jon

Reputation: 6541

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

Upvotes: 10

JW.
JW.

Reputation: 4951

If you prefer to use the simplest possible solution to a problem, an alternative to RedirectMatch is, the more basic, Redirect directive.

It does not use pattern matching and so is more explicit and easier for others to understand.

i.e

<IfModule mod_alias.c>

#Repoint old contact page to new contact page:
Redirect 301 /contact.php http://example.com/contact-us.php

</IfModule>

Query strings should be carried over because the docs say:

Additional path information beyond the matched URL-path will be appended to the target URL.

Upvotes: 0

Gumbo
Gumbo

Reputation: 655129

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

Upvotes: 90

Ryall
Ryall

Reputation: 12281

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

Upvotes: 0

duckyflip
duckyflip

Reputation: 16489

This should do it

RedirectPermanent /contact.php /contact-us.php 

Upvotes: 22

Related Questions