I need to redirect an URL with a variable, eg.

http://www.mydomain.com/foo/55555 should go to http://www.mydomain.com/bar/55555 where 55555 is a variable number.

Do I need to use redirect or rewrite?

  • 1
    You need a rewrite to handle the variable. You need a redirect if you want users who visit foo, to go to bar. – SPRBRN Mar 4 '15 at 9:29

here's a RewriteRule that implements the redirect ([R])

  RewriteRule ^/foo/([0-9]*)$ /bar?q=$1 [R]

if you are using .htaccess to add your rewrite rules, you might want to make this local to the /foo directory, so put the following into foo/.htaccess:

  RewriteRule ^([0-9]*)$ /bar&q=$1 [R]
| improve this answer | |
  • I have insert this rule RewriteRule http://www.mydomain.com/foo/([0-9]*)$ http://www.mydomain.com/bar?q=$1 [R=301] but don't work – hellb0y77 Mar 4 '15 at 9:44
  • @hellb0y77 so why didn't you copy my rule? – umläute Mar 4 '15 at 9:45
  • have you restarted apache? what do your logs say? if you want a decent answer, you need to provide more than "didn't work"... – umläute Mar 4 '15 at 9:49
  • @hellb0y77 btw, why do you suddenly change the rewrite rule to a different target? – umläute Mar 4 '15 at 9:55
  • From log: [04/Mar/2015:10:55:10 +0100] "GET /foo/55555 HTTP/1.1" 404 1441 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36" The rule is in .htaccess – hellb0y77 Mar 4 '15 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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