homepage Welcome to WebmasterWorld Guest from 54.227.146.68
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
mod rewrite removing underscore help please
musicales




msg:4226157
 8:12 pm on Nov 3, 2010 (gmt 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 ?

 

sublime1




msg:4226304
 12:51 am on Nov 4, 2010 (gmt 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 :-)

Tom

g1smd




msg:4226320
 1:22 am on Nov 4, 2010 (gmt 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?

musicales




msg:4226596
 2:21 pm on Nov 4, 2010 (gmt 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!

jdMorgan




msg:4236368
 6:07 pm on Nov 29, 2010 (gmt 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.

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved