[php] .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:

  • /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?

This question is related to php .htaccess seo http-status-code-301

The answer is


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


It will redirect your store page to your contact page

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

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.


This should do it

RedirectPermanent /contact.php /contact-us.php 

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


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to .htaccess

Use .htaccess to redirect HTTP to HTTPs Getting a 500 Internal Server Error on Laravel 5+ Ubuntu 14.04 Server unable to read htaccess file, denying access to be safe Laravel 5 – Remove Public from URL Laravel 5 not finding css files How can I fix the 'Missing Cross-Origin Resource Sharing (CORS) Response Header' webfont issue? How Can I Remove “public/index.php” in the URL Generated Laravel? Apache 2.4 - Request exceeded the limit of 10 internal redirects due to probable configuration error Forbidden You don't have permission to access / on this server Htaccess: add/remove trailing slash from URL

Examples related to seo

Can a relative sitemap url be used in a robots.txt? How do search engines deal with AngularJS applications? What is the purpose of the "role" attribute in HTML? How to request Google to re-crawl my website? When should I use a trailing slash in my URL? Redirecting 404 error with .htaccess via 301 for SEO etc .htaccess 301 redirect of single page Replacing H1 text with a logo image: best method for SEO and accessibility?

Examples related to http-status-code-301

How long do browsers cache HTTP 301s? PHP header redirect 301 - what are the implications? Redirecting 404 error with .htaccess via 301 for SEO etc .htaccess 301 redirect of single page HTTP redirect: 301 (permanent) vs. 302 (temporary) How do I make a redirect in PHP?