Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod rewrite removing underscore help please

8:12 pm on Nov 3, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:May 8, 2002
posts: 440
votes: 0

I'd like to convert http://www.example.com/products/11_1234.asp into http://www.example.com/products/?category_Id=11&product_Id=1234

but I'm really struggling with the regular expressions, particular because of the underscore.
Can anyone help me out ?
12:51 am on Nov 4, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:June 10, 2003
posts: 410
votes: 0

So I'll assume you're doing a rewrite rule to do this transformation and that you are doing a permanent external redirect. This may not be the case and if so, you may also need to use a RewriteCond. But the basic idea of the regular expression will be similar:

RewriteRule ^/?products/([0-9]+)_([0-9]+)\.asp$ http://www.example.com/products/?category_Id=$1&product_Id=$2 [R=301,L]

The RewriteRule has as it's first argument the incoming path of the request (not the domain, not the query string if any). It may or may not have the leading slash depending on whether your context is .htaccess or server level config. This code works either way.

This reads something like: if the path of the incoming request possibly starts with a slash, then "products/", then one or more digits, then an underscore, then one or more digits, then ends with ".asp" do an external redirect using HTTP 301 status (R=301), returning a URL that has the first set of digits after category_Id= and the second set after product_Id=, and stop processing more rewrite rules (L)

The parenthesis act in this context as a "capture group" and pick up all the matching characters for use later in the $1, $2 variables.

If you want to do this without the user knowing about it, it gets a little trickier -- an internal rewrite. If so, and you need help, please let us know if this is in the context of .htaccess or can be done within the server configuration (probably within a <VirtualHost> container). This example also assumes there's no existing query string you need to retain on the incoming URL -- if so, look at the QSA flag.

Hopefully I got this about right. My hit rate on these is about 30%, so I strongly caution you from assuming I did. I learned that "new math" where it was the idea that counted :-)

1:22 am on Nov 4, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
votes: 0

Post delayed 4 hours:

What do you mean by convert?

Is this a redirect or a rewrite?

Which one of those is the URL as requested by the browser? If the other one a redirected URL, or is it merely the internal filepath where the content resides?
2:21 pm on Nov 4, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:May 8, 2002
posts: 440
votes: 0

Thanks for your help sublime1.
I would like to do an internal rewrite. So I would like the browser to request /products/12_1234.asp and get for the server to behind the scenes actually serve (without being visible to the browser) the page /product/?category_id=12&product_Id=1234 I'd also like it to check in case there are extra querystrings. Do I put something like [QSA,I,L] instead of your [R=301,L]
to acheive that?
Thanks again for your help.

I also need to cover the possibility of querystrings being added
So would that be something like:

RewriteRule ^/?products/([0-9]+)_([0-9]+)\.asp$ http://www.example.com/products/?category_Id=$1&product_Id=$2 [QSA,L]

I saw somewhere else [QSA,I,L] at the end, but I'm not sure what the difference is.
Your help is really appreciated!
6:07 pm on Nov 29, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
votes: 0

Please tell us where you are putting this code, so we can keep it simple. For use in example.com/.htaccess, I'd suggest:

RewriteRule ^products/([0-9]+)_([0-9]+)\.asp$ /products/?category_Id=$1&product_Id=$2 [QSA,L]

The code you found with "[QSA,I,L]" was likely for ISAPI Rewrite -- the Windows IIS server equivalent of Apache mod_rewrite. The code is similar, but not interchangeable.