0

I wish to rewrite and 301 redirect a URL with query parameter like the below:-

domain.com/link.asp?StockNo=601-0287

to something such as the below:-

domain.com/Product-Name

The stock number will be different for many URL's as will the Product-Name for each.

There is not many URL's to do so manually writing would not be an issue...

Something such as the below is not working:-

RewriteRule ^link.asp/?StockNo=601-0287$ /Product-Name/$1 [R=301,L]

But this would be the kind of desired rules.

What would be the best way to handle these rewrites?

Would a RewriteCond be required? I'm presuming {QUERY_STRING} wouldn't be required because the Product-Name does not match the query string?

Thanks in advance.

zigojacko
  • 1,983
  • 9
  • 45
  • 77

1 Answers1

1

To match REQUEST_URI and QUERY_STRING together you need to use %{THE_REQUEST}

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+link\.asp\?StockNo=[^\s]+ [NC]
RewriteRule ^ /Product-Name/? [R=302,L]

EDIT: To handle specific stocks numbers:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+link\.asp\?StockNo=601-0287 [NC]
RewriteRule ^ /Product-A/? [R=302,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+link\.asp\?StockNo=601-0293 [NC]
RewriteRule ^ /Product-B/? [R=302,L]

If you have too many of these stocks to rewrite then I would strongly recommend using RewriteMap to you. See this answer for more details: Edit .htaccess with PHP

Community
  • 1
  • 1
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks for your reply. Almost so nearly there - It's appending the URL with the query string (i.e: `domain.com/Product-Name/?StockNo=601-0287`). Everything from the `?` is not required. Is there a modification to your answer that would remove the query string from the URL? Thanks for such a prompt answer. – zigojacko May 29 '13 at 15:32
  • Can you try in a different browser since this `?` in the `/Product-Name/%1?` would strip down query from redirected URL. – anubhava May 29 '13 at 15:34
  • Same occurred in different browser (cache was cleared just to be sure). – zigojacko May 29 '13 at 15:36
  • I also tested in my Apache and redirected URL came out to be: `/Product-Name/601-0287` so can't think of a reason why it is different for you. Can you pls post your latest .htaccess (with this change) to your question. – anubhava May 29 '13 at 15:38
  • If that is the outcome for you, please could you modify it to remove the number after `Product-Name/`? It doesn't need any of the query string at the end of the URL. Thanks. If I can figure out how to initialise the chat window with you, I will be able to show you the .htaccess and a live URL where this is happening :) Thanks. – zigojacko May 29 '13 at 15:46
  • 1
    Alright I edited to make it `/Product-Name/` can you try it now. – anubhava May 29 '13 at 15:49
  • That's done it, working perfectly now. Thanks very much. Great work! :) – zigojacko May 29 '13 at 15:50
  • Oh wait, it's redirecting all stock numbers to the same page. (For example: `link.asp/?StockNo=601-0293` also goes to the same Product-Name). – zigojacko May 29 '13 at 15:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30848/discussion-between-geoff-jackson-and-anubhava) – zigojacko May 29 '13 at 15:53
  • Yes it will be redirecting to literal string `/Product-Name/` since .htaccess rules don't know how to translate to names from StockNo. Precisely that's why I included StockNo in the URL earlier. – anubhava May 29 '13 at 15:53
  • 1
    Revised edit to handle individual stock numbers to different products works perfectly. Thanks. Super answer. – zigojacko May 29 '13 at 16:23