0
votes

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.

1

1 Answers

1
votes

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