FREE THOUGHT · FREE SOFTWARE · FREE WORLD

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 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://static.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]

Tags

March 2nd, 2007

Comments Welcome

  • 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

  • http://www.askapache.com/ AskApache
    RedirectMatch 301 ^/(.*)posts(.*)$ http://www.askapache.com/$1donations$2
  • 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.

  • http://www.liewcf.com Liew Cheon Fong

    Great! Thank you very much! It solved my problem to redirect /blog/blog-posts/ to /blog-posts/ :)

  • Dan

    How do you redirect an exe or zip file?

    I've tried:

    Redirect 301 http://www.site.com/old/software.exe http://www.site.com/new/software.exe

    But it doesn't work...

  • http://ewebcraft.com/ Atif

    Thanks, this is what i needed.

    Just one thing, if new and old urls are both on the same domain, why it does not work without full new/destination URL:

    Redirect 301 /some-page.html /new-page.html

    It does not work. you have to write:

    Redirect 301 /some-page.html  http:www.example.com/new-page.html
  • http://www.headstonesandmemorials.com/Headstones.php JT Stone

    Thanks for this info. I was looking for this information - so glad I found it.

  • http://www.motorcyclemechanicschool.net maguai

    I used your link above as a guide to redirect all pages with a slash at the end to .html
    RedirectMatch 301 ^/(.*)/$ http://www.example.net/$1.html
    but google says I still have pages without the slash (previously wordpress) that show 404. I tried
    RedirectMatch 301 ^/(.*)$ http://www.example.net/$1.html
    but this didn't work. any ideas. I have 2 other site to redirect, some with 100+ pages so individually wont work. Ive looked anywhere, any idea? Thanks!

  • cameron

    Any idea why this would not be rewriting?

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^something.example.com$
    RewriteRule ^(.*)$ http://somewhere.example.net/someplace/ [R=301,L]

    The redirect is working BUT it is appending the original requested location onto the destination.

    So for example without all the subdomains and directories just to show what is being appended:

    http://example.com/?2087
    is supposed to just go to
    http://example.net
    but it is going to:
    http://example.net/?2087

    The same code works as expected on some of my servers but not on others. Could another redirect or directory entry in httpd.conf be overriding this ion the ones that are not working?

  • trolololo

    I think you have a couple extra "(" and one too many "/", also adjust your forth "*" and second "^"

  • vikas

    I want search pattern in URL e.g "abc" & then if pattern exists redirect to static file on server.Can you please suggest syntax for it.

  • vikas

    I want search pattern in URL e.g "abc" & then if pattern exists redirect to static file on server.Can you please suggest syntax for it.

  • quickQ

    can you use both mod-alias and mod-rewrite in the same htaccess file or will problems occur?

Popular Articles
My Online Tools

Related Articles
Newest Posts
Twitter


  • @askapache · Jul 21
    Magic spells for sending thoughts across time? Books
  • @askapache · Jul 20
    TV is just a relic of the previous generation. We just don't know it yet.
  • @askapache · Jul 20
    I will never go back on my ideals, no matter the cost. I'll never let the economic vultures steal my dreams. I'd rather give up the ghost
  • @askapache · Jul 18
    I don't want a better seat, I want control of the engine
  • @askapache · Jul 14
    No matter how good u r, there will always be someone 2x, 5x, 100x better. This is true for me and everyone. No direction but forward.
  • @askapache · Jul 12
    Heads up, I'll DDoS the f out of askapache next week, to see how resilient it really is :) - will try to overflow disk, net, and ip stack
  • RUN GCC! This is a typical shirt I wear, from the  t.co/46LYbFr4k2  shop. A clerk at the LQ recognized it!  t.co/jjmT0dkCPu 
  • Merlin the Magician  t.co/iMmRbanUi4 
  • ROGUE CODE - Latest novel from @markrussinovich  t.co/apkn0LoPIt 
  • RTFM - surprisingly very helpful and way more comprehensive than it looks! @redteamfieldman #pwnAllTheThings  t.co/xiaJ5g0aC9 
  • Dear Hacker - Letters to the Editor of 2600, from Emmanuel Goldstein  t.co/JCfLab7FAJ 
  • The Mythical Man-Month - Essays on Software Engineering, by Frederick P. Brooks, Jr.  t.co/ilWN5GHElr 
  • "where wizards stay up late" - The Origins of the Internet. Favorite book detailing the birth of the net and IMPs  t.co/gY9VTGJgZz 
  • ZERO DAY - read before Trojan horse  t.co/pPMLGDJv8P 
  • Trojan Horse, a novel!  t.co/Hf8EtYaZVa 
  • The Hacker Playbook - very nice high level overview of attacks  t.co/lHwNVWi61u 
  • Clean Code - A Handbook of Agile Software Craftsmanship  t.co/hnJX0x1qIc 
  • Secrets of the JavaScript Ninja - By my absolute favorite JS hacker John Resig!  t.co/tZ42ljmcCl 
  • Hacking Exposed 7: Network Security Secrets & SolutionsMy all time favorite, basic but thorough and accurate.  t.co/jycW0RDVtZ 
  • Empty words will be no surrogate for cold resolve. Pain is nothing.  t.co/qXjpRxbjCw 

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






[hide]

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.

| Google+ | askapache

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

↑ TOPMain