301 Redirect with mod_rewrite or RedirectMatch

FREE THOUGHT · FREE SOFTWARE · FREE WORLD

301 Redirect with mod_rewrite or RedirectMatch

The Apache Web Server provides a couple ways to issue 301 Redirects from within .htaccess or httpd.conf files.

When do you need a 301 Redirect?

  • When you change your domain name
  • When you change a URL
  • When external links pointing to your site or pages are old or invalid

301 Redirects using mod_alias

Using mod_alias we have access to these directives: Redirect, RedirectMatch, RedirectPermanent, and RedirectTemp. You only need to know that RedirectMatch allows pattern matching and Redirect is only for a single file.

301 Redirects using Redirect

Redirect 301 /2007/webmaster/custom-phpini-with-fastcgi-on-dreamhost.html http://www.askapache.com/dreamhost/custom-phpini-with-fastcgi-on-dreamhost.html
Redirect 301 /2007/phpbb/sending-post-form-data-with-php-curl.html http://www.askapache.com/htaccess/sending-post-form-data-with-php-curl.html
Redirect 301 /htaccess.txt /wp-content/uploads/p/htaccess.txt
Redirect 301 /2007/webmaster/php-and-ajax-shell-console.html http://www.askapache.com/tools/php-and-ajax-shell-console.html
Redirect 301 /2007/webmaster/custom-phpini-with-fastcgi-on-dreamhost.html http://www.askapache.com/dreamhost/custom-phpini-with-fastcgi-on-dreamhost.html

301 Redirects using RedirectMatch

RedirectMatch 301 ^/(.*)\.htm$ http://www.askapache.com/$1.html
RedirectMatch 301 ^/200([0-9])/([^01])(.*)$ http://www.askapache.com/$2$3
RedirectMatch 301 ^/category/(.*)$ http://www.askapache.com/$1
RedirectMatch 301 ^/(.*)/htaccesselite-ultimate-htaccess-article.html(.*) http://www.askapache.com/htaccess/htaccess.html
RedirectMatch 301 ^/(.*)\.html/1/(.*) http://www.askapache.com/$1.html$2
RedirectMatch 301 ^/manual/(.*)$ http://www.php.net/manual/$1
RedirectMatch 301 ^/dreamweaver/(.*)$ http://www.askapache.com/tools/$1
RedirectMatch 301 ^/z/(.*)$ http://z.askapache.com/$1

301 Redirects with mod_rewrite

mod_rewrite is a powerful Apache module, and you should read more about it before you cause a problem.

301 Redirects using mod_rewrite

RewriteRule .* http://www.askapache.com/$1 [R=301,L]
 
RewriteRule ^(.*)\.html([^c]+)comment-(.+)$ http://www.askapache.com/$1.html#comment-$3 [R=301,L,NE]

«
»

Skip to Comments

Add Your Opinion

Reader Comments

  1. B George ~

    Hi,

    I’m using a tool to crawl my web pages. And the tool says I’ve duplicate pages. The duplicates are below:

    1. /
    2. /index.php

    Is it necessary and possible to redirect (1) to (2)?

    Thanks,
    Biju.

  2. AskApache ~
    RedirectMatch 301 ^/(.*)posts(.*)$ http://www.askapache.com/$1donations$2
  3. kitten13 ~

    Hello,

    I try desperately to apply your advice without success, I need to replace the words posts by donations in my url.

    So I did this but this does not :

    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?\.html$ [NC]
    RewriteRule ^/(.*)$ ([-]?[0-9]+)([-_][^/]*)?/dons/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?\.html$1 [L,R=301]

    or

    RedirectMatch 301 ^([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?\.html$
     
    /([-]?[0-9]+)([-_][^/]*)?/dons/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?\.html

    Could you tell me where is my mistake.

    Thank you for your help

    best regards


It's very simple - you read the protocol and write the code. -Bill Joy

HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C | TLDP | WAI | DISA | ICSI | GIAC | SANS RR | GHOST | DEFCON | NIST | DHS CYBER | NIST | .:: Phrack Magazine ::.

↑ TOPExcept where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. HTTPD based on NCSA HTTPd

Site Map | Contact Webmaster | Email AskApache | Glossary | License and Disclaimer | Terms of Service