58

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?

Dhaust
  • 5,470
  • 9
  • 54
  • 80
Dan
  • 916
  • 2
  • 7
  • 10

7 Answers7

88

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
  • 643,351
  • 109
  • 780
  • 844
  • When I do this with something like www.domain.com/contact.php?location=12 it redirects to www.domain.com/home/mike/public_html/contact-us.php?location=12 but only when there's the arguments on there. – Mike Mar 19 '13 at 19:02
  • 1
    I fixed it by putting in the full URL (with http://) in the destination side so: "RedirectMatch 301 ^/contact\.php$ http://www.domain.com/contact-us.php". – Mike Mar 19 '13 at 21:21
  • instead of `/contact-us.php` you could use ANY url. I needed to redirect to an IP address – clod986 Mar 19 '15 at 10:11
  • hey amazing but I would to do something similar but I would like that when someone try get in a section private from my hosting then redirect to other page like > private.php login.php but before I hidden my file with this command Require all denied then how could do that? – simon Jan 27 '20 at 06:40
  • Just a note, this is case sensitive only – DomainsFeatured Mar 30 '20 at 19:04
  • After redirecting that way, the url will end with extra "?page=" parameter. Anyone knows how to remove this extra parameter ? – 10101101 May 15 '20 at 19:56
22

This should do it

RedirectPermanent /contact.php /contact-us.php 
duckyflip
  • 16,189
  • 5
  • 33
  • 36
  • I can’t see why this should solve the problem. `RedirectPermanent …` is the same as `Redirect 301 …`. – Gumbo Sep 14 '09 at 11:56
  • 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 Sep 14 '09 at 11:59
  • @duckyflip: Ah sorry, you’re right. Misread the `RedirectMatch` with just `Redirect`. But `Redirect` does only a prefix check. – Gumbo Sep 14 '09 at 12:03
9
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

Jon
  • 6,437
  • 8
  • 43
  • 63
  • Simple is better. If you don't need a pattern (regex) then this would be the most efficient syntax. – jeffkee Aug 11 '17 at 21:10
0

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

Ryall
  • 12,010
  • 11
  • 53
  • 77
0

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.

JW.
  • 4,821
  • 5
  • 43
  • 60
0

It will redirect your store page to your contact page

    <IfModule mod_rewrite.c>
    RewriteEngine On 
    RewriteBase /
    Redirect 301 /storepage /contactpage
    </IfModule>
gurcharan
  • 23
  • 3
0

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]
chispitaos
  • 767
  • 9
  • 14