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 http://z.askapache.com/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/apache-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.
- mod_rewrite questions and answers forum filled with example mod_rewrite code
- mod_rewrite Tips and Tricks
- URL Rewriting Guide
- Replacing ˜%23² with ˜#’ in incoming links
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]
RewriteRule RewriteCond RewriteEngine RewriteBase
Related Articles