Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

RedirectMatch 301 ^/([^/]+)/([^/]+)/$ http://www.example.com/$2

What it basically does is, redirect http://www.mysite.com/sports/test-post/ to http://www.mysite.com/test-post/.

Now, how do I modify the .htaccess rule to do the opposite? (i.e. redirect http://www.mysite.com/test-post/ to http://www.mysite.com/sports/test-post/)

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted
RewriteCond %{REQUEST_URI} !^/sports/ [NC]
RewriteRule ^/([^/]+)/(.*)$ http://www.example.com/sports/$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
add comment

Your Answer

 
discard

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.