Home  »  Htaccess  »  301 Redirect with mod_rewrite or RedirectMatch

by 13 comments

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 /dreamhost/custom-phpini-with-fastcgi-on-dreamhost.html
Redirect 301 /2007/phpbb/sending-post-form-data-with-php-curl.html /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 /tools/php-and-ajax-shell-console.html
Redirect 301 /2007/webmaster/custom-phpini-with-fastcgi-on-dreamhost.html /dreamhost/custom-phpini-with-fastcgi-on-dreamhost.html

301 Redirects using RedirectMatch

RedirectMatch 301 ^/(.*).htm$ /$1.html
RedirectMatch 301 ^/200([0-9])/([^01])(.*)$ /$2$3
RedirectMatch 301 ^/category/(.*)$ /$1
RedirectMatch 301 ^/(.*)/htaccesselite-ultimate-htaccess-article.html(.*) /htaccess/htaccess.html
RedirectMatch 301 ^/(.*).html/1/(.*) /$1.html$2
RedirectMatch 301 ^/manual/(.*)$$1
RedirectMatch 301 ^/dreamweaver/(.*)$ /tools/$1
RedirectMatch 301 ^/z/(.*)$$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 .* /$1 [R=301,L]

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


March 2nd, 2007

Comments Welcome

Related Articles

My Online Tools
Popular Articles

Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman


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

Except 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. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | askapache

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service

↑ TOPMain