Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let me explain by example. Say, I have this redirection rule in my .htaccess file:

RedirectMatch 301 ^/([^/]+)/([^/]+)/$$2

What it basically does is, redirect to

Now, how do I modify the .htaccess rule to do the opposite? (i.e. redirect to

share|improve this question
up vote 2 down vote accepted
RewriteCond %{REQUEST_URI} !^/sports/ [NC]
RewriteRule ^/([^/]+)/(.*)$$1/$2 [R=301]

This'll also send /test-post/blah/ to /sports/test-post/blah - if you don't want to do the redirect when there's extra stuff on there, ditch the (.*) from the match string and the $2 from the replace string.

share|improve this answer
Oops! That was just an example. sports can actually be any category depending on the page. So, sports can't really be in the rule. EDIT: Also, if possible, please provide a RedirectMatch rule instead of mod_rewrite-based one. Thanks! – its_me Jul 4 '12 at 19:46
@AahanKrish You'll need to clarify what your aim is, then. How's the redirect supposed to get the correct category, when all it has is /test-post? Regarding RedirectMatch, it can only work if you'll only ever redirect for /test-post/ - if you want to redirect for /test-post/blah then RedirectMatch will just give you a redirect loop. – Shane Madden Jul 4 '12 at 19:51
:P How stupid of me. Thanks for your answer, it solves my problem. – its_me Jul 4 '12 at 19:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.