2

I'm a complete n00b when it comes to regular expressions. I need these redirects:

(1)

www.mysite.com/bike.php?id=001&product=Product-Name&source=Source-Name
should become -> www.mysite.com/Source-Name/001-Product-Name

(2)

www.mysite.com/car.php?id=002&product=Product-Name&source=Source-Name
should become -> www.mysite.com/Source-Name/002-Product-Name

(3)

www.mysite.com/moto.php?id=005&product=Product-Name&source=Source-Name
should become -> www.mysite.com/Source-Name/005-Product-Name

(4)

www.mysite.com/stores.php?id=002&name=Store-Name
should become -> www.mysite.com/002-Store-Name

Edit: I should have clarified, there are 3 product pages, which should all redirect to the same format URL

Any help much appreciated :)

3
  • 1
    Are you sure you don't need them the other way around? Most people do. Commented Jun 11, 2010 at 7:03
  • This time you got lucky, but mostly when asking questions on the internet you can not expect other people to do your work. Other people are willing to help you, and it helps if you show that you have taken some effort to solve the problem. In your case, you could show us the RewriteRule you came up with. Even if it is totally wrong, you have shown that you tried it yourself. Commented Jun 11, 2010 at 7:31
  • @Iganacio - Heh, you're right.. That's why the regexs haven't been working. Again, noob :p Thanks for the clarification Commented Jun 11, 2010 at 7:44

2 Answers 2

1
RewriteEngine On
RewriteRule ^(\d+)-(.+)$ /stores.php?id=$1&name=$2 [L]
RewriteRule ^(.+)/(\d+)-(.+)$ /products.php?id=$2&product=$3&source=$1 [L]
Sign up to request clarification or add additional context in comments.

9 Comments

Sorry, how would I reverse those rules? Thanks :)
You mean that the user should enter and see the long & ugly URL and it has to be silently transformed in the short and clean one? It's the exact opposite of what everyone does: giving a visible "nice" URL and let the server handle the dirty stuff under the hood.
Eg. the browser shows "mysite.com/Source-Name/001-Product-Name" in the url bar, but the actual page is feeding from mysite.com/…
That's exactly what the rules I wrote do :)
hmm.. doesn't seem to be working.. Would it matter if everything's in the /dev directory? eg. mysite.com/dev/products...
|
1
RewriteCond %{REQUEST_URI} www.mysite.com/(.+)/(\d+)-(.+)  /products.php?id=$2&product=$3&source=$1

RewriteCond %{REQUEST_URI} www.mysite.com/(\d+)-(.+)  /products.php?id=$1&name=$2

I think it work's well ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.