1

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
  • 1
    You need a rewrite to handle the variable. You need a redirect if you want users who visit foo, to go to bar. Commented Mar 4, 2015 at 9:29

1 Answer 1

2

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]
7
  • 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 Commented Mar 4, 2015 at 9:44
  • @hellb0y77 so why didn't you copy my rule? Commented Mar 4, 2015 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"... Commented Mar 4, 2015 at 9:49
  • @hellb0y77 btw, why do you suddenly change the rewrite rule to a different target? Commented Mar 4, 2015 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 Commented Mar 4, 2015 at 10:03

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.