0

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/)

1 Answer 1

2
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.

3
  • 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, 2012 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. Jul 4, 2012 at 19:51
  • :P How stupid of me. Thanks for your answer, it solves my problem.
    – its_me
    Jul 4, 2012 at 19:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .