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

Table of Contents

  1. Introduction
    1. 301 Redirects using mod_alias
      1. 301 Redirects using Redirect
      2. 301 Redirects using RedirectMatch
    2. 301 Redirects with mod_rewrite
      1. 301 Redirects using mod_rewrite

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]

301 Redirect with mod_rewrite or RedirectMatch - AskApache

Charles Torvalds
2 Mar 2007

  • Site Map WireShark GNU Non-GNU Tor Project cURL TLDP - Documentation
  • Htaccess Files Hacking Htaccess Javascript Linux Optimization PHP Security Shell Scripting WordPress
  • Base64 Image Converter Raw HTTP Header Debugger Graphical ASCII Text Generator Mac Address Vendor Lookup Who Am I – Your IP Information Request Method Security Scanner .htpasswd file Generator Compress CSS DNS Tracer
Copyright © 2025 AskApache
  • Site Map
  • Htaccess Files
  • Hacking
  • Htaccess
  • Javascript
  • Linux
  • Optimization
  • PHP
  • Security
  • Shell Scripting
  • WordPress
  • Base64 Image Converter
  • Raw HTTP Header Debugger
  • Graphical ASCII Text Generator
  • Mac Address Vendor Lookup
  • Who Am I – Your IP Information
  • Request Method Security Scanner
  • .htpasswd file Generator
  • Compress CSS
  • DNS Tracer
Exit mobile version