10

How do I 301 redirect, for example: a subdirectory /Blog/ to /blog/ with .htaccess?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Wesley
  • 101
  • 1
  • 3

3 Answers3

9
Redirect 301 /Blog /blog

Or use something like http://www.htaccessredirect.net/index.php

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Sam Plus Plus
  • 4,381
  • 2
  • 21
  • 43
3

The way that immediately comes to mind:

RewriteEngine on
RewriteBase /path/to/your/web/app
RewriteRule ^Blog$ blog [R=301,L]
RewriteRule ^Blog/(.*)$ blog/$1 [R=301,L]

There are probably much better ways than mod_rewrite, and I'm not 100% sure that the external redirects will work as they should -- you may need the full URL -- but there you go.

Andrew
  • 14,325
  • 4
  • 43
  • 64
2

This is the simplest .htaccess solution, place it in /.htaccess:

Redirect 301 /Blog /blog

But it's really limited. If you want to catch every possible CaSe-wise misspelling, and also forward any other path info (such as /Blog/foo/bar.html), use this instead:

RedirectMatch 301 ^/[Bb][Ll][Oo][Gg](?<!blog)(/.*)?$ /blog$1

For more options, there are full .htaccess generators available.

Or you can use ModRewrite-based rules for maximum flexibility, but it's probably overkill.

Jay Dansand
  • 789
  • 1
  • 9
  • 13
  • Your second example will also match `/blog` at any position. – Gumbo Oct 27 '10 at 21:06
  • Very correct - I rushed to hit the Add Answer too quickly. Thanks for pointing it out! The REGEX is fixed now. – Jay Dansand Oct 27 '10 at 21:17
  • Yes indeed this one is way better althouth there alway the famous: "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. " – Necronet Oct 27 '10 at 22:01