-1

I have moved site from one framework to another and now I would like to redirect important pages (around 20). How to redirect them using apache rewrite? Here are examples:

Old: http://mydomain.com/ba/stream.php?kat=15
New: http://www.mydomain.com/bs/about-us/our-partners

Old: http://mydomain.com/ba/stream.php?kat=29
New: http://www.mydomain.com/bs/catalogues/it-catalogue

Also, after important pages are listed, I'd like to redirect all remaining links in form:

http://mydomain.com/ba/whatever-is-here

to

http://www.mydomain.com/bs/

Following TerryE's suggestion, I'll attach my latest code which is not working :)

RewriteCond %{QUERY_STRING} ^kat=15$
RewriteRule ^stream\.php$ http://www.mydomain.com/bs/about-us/our-partners [R=301,L]

One more note: I use this code to redirect root domain to www subdomain:

RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

I have placed this root to www code below code for specific pages redirection, and when I test http://mydomain.com/ba/stream.php?kat=15 in browser, I am redirected to http://www.mydomain.com/bs/ba/stream.php?kat=15

Adnan Kurtovic
  • 135
  • 2
  • 2
  • 8
  • What have you tried or researched? Have you read any rewrite examples using `%{QUERY_STRING}` conditions? We are here to help, not to do your thinking for you. – TerryE Aug 10 '12 at 23:58
  • I am sorry if I have offended you. I spent hours researching, trying and testing with no luck. That is why I asked. I know this should be easy to do, that is why I have not attached any code I have tried. – Adnan Kurtovic Aug 11 '12 at 01:14
  • @AdnanKurtovic: Showing your attempts, even if they don't work, a) shows a good faith effort on your part, b) helps answerers understand what you're trying to do, and c) helps answerers who might have tried the same thing (in some cases, it might not be as easy as you think it is). – David Robinson Aug 11 '12 at 01:34
  • Note taken. I listened and edited my question. – Adnan Kurtovic Aug 11 '12 at 01:50
  • Adnan, no you haven't offended me and sorry if I implied that. It's just the question form points you to guidelines [asking help »](http://stackoverflow.com/questions/ask) which we expect you to read. If you can't be bothered to do this, then perhaps you shouldn't be surprised if we can't be bothered to reply :-) – TerryE Aug 11 '12 at 09:54

1 Answers1

1

Adnan,

You need to add the following lines to your DOCROOT/.htaccess. This assumes that you don't have any .htaccess file in your ba subdirectory. (See Tips for debugging .htaccess rewrite for an explanation of why.)

RewriteEngine On
RewriteBase   /
#
# Redirect kat 15 to the Partners page 
#
RewriteCond %{QUERY_STRING}   ^kat=15$
RewriteRule ^ba/stream\.php$  http://www.mydomain.com/bs/about-us/our-partners? [R=301,L]
#
# Redirect all other kat ids to the corresponding bs page 
#
RewriteCond %{QUERY_STRING}   ^kat=(\d+)$
RewriteRule ^ba/stream\.php$  http://www.mydomain.com/bs/%1? [R=301,L]

Note:

  • the rule matches against the UPI less the leading path (in this case /)
  • you need to have the trailing ? on the replacement string to suppress the existing query parameters.
  • the %n parameters pick up the match variables from the last successful condition match.
Community
  • 1
  • 1
TerryE
  • 10,724
  • 5
  • 26
  • 48
  • Thank you @TerryE your suggestion really did the trick. One more thing that is left is to redirect all other pages in format `http://mydomain.com/ba/stream.php?kat=12`, where 12 represents any number to subfolder `bs`. Could you help me with this please, as well? I have tried adding this code `RewriteRule ^ba/* /bs/$1 [R=301,L]` but it does not work. I have tried `RewriteRule ^ba/(.*)$ /bs/$1 [R=301,L]` as well with similar result :) – Adnan Kurtovic Aug 15 '12 at 17:27
  • @AdnanKurtovic, I've added the extra lines that you need. It would help if you read the mod_rewrite docs which explain this ;-? BTW how about a tick for ans :-) – TerryE Aug 15 '12 at 20:58
  • thank you very much. When I need to use this I usually solve that particular problem I have, but I will definitely have to sit and learn it properly. Thanks once more. – Adnan Kurtovic Aug 16 '12 at 08:29