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 https://www.askapache.com/dreamhost/custom-phpini-with-fastcgi-on-dreamhost/ Redirect 301 /2007/phpbb/sending-post-form-data-with-php-curl.html https://www.askapache.com/htaccess/sending-post-form-data-with-php-curl/ Redirect 301 /htaccess.txt /wp-content/uploads/p/htaccess.txt Redirect 301 /2007/webmaster/php-and-ajax-shell-console.html https://www.askapache.com/tools/php-and-ajax-shell-console/ Redirect 301 /2007/webmaster/custom-phpini-with-fastcgi-on-dreamhost.html https://www.askapache.com/dreamhost/custom-phpini-with-fastcgi-on-dreamhost/
301 Redirects using RedirectMatch
RedirectMatch 301 ^/(.*).htm$ https://www.askapache.com/$1.html RedirectMatch 301 ^/200([0-9])/([^01])(.*)$ https://www.askapache.com/$2$3 RedirectMatch 301 ^/category/(.*)$ https://www.askapache.com/$1 RedirectMatch 301 ^/(.*)/htaccesselite-ultimate-htaccess-article.html(.*) https://www.askapache.com/htaccess/ RedirectMatch 301 ^/(.*).html/1/(.*) https://www.askapache.com/$1.html$2 RedirectMatch 301 ^/manual/(.*)$ http://www.php.net/manual/$1 RedirectMatch 301 ^/dreamweaver/(.*)$ https://www.askapache.com/tools/$1 RedirectMatch 301 ^/z/(.*)$ https://www.askapache.com/s/s.askapache.net/$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 .* https://www.askapache.com/$1 [R=301,L] RewriteRule ^(.*).html([^c]+)comment-(.+)$ https://www.askapache.com/$1.html#comment-$3 [R=301,L,NE]
« Control htaccess Basic Authentication with PHP and mod_rewrite27 Request Methods for Apache rewritecond htaccess »
Comments