homepage Welcome to WebmasterWorld Guest from
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 Rule Problem
Converting html request to dynamic php page URI

 5:06 pm on Aug 6, 2003 (gmt 0)

I've been browsing a number of threads here about the dreaded Mod Rewrite, and can't seem to get my version correct. I've looked at [webmasterworld.com...] amongst other threads for hints.

I've got my site set up with a custom shop, using product pages of the form:


I'm now trying to write a RewriteRule to convert requests for the above page to:


So far I've got the following rule, but it results in a 404!

RewriteRule ^shop/([0-9]+)\-([0-9]+)\.html$ /shop/product.php?asin=$1&ukus=$2 [L]

Also, I cannot be sure that the product code will always take the same form; sometimes it may be 020530902X, and others it might be 020530902.

My questions are...

1/ What is the correct rule?

2/ Should I place the rule in my main .htaccess file, or can I have a dedicated file in the /shop/ directory?

Thanks in advance :-)



 5:29 pm on Aug 6, 2003 (gmt 0)


> convert /shop/020530902X-us.html to /shop/product.php?asin=020530902X&ukus=us

RewriteRule ^shop/([0-9]{9})X?-([a-z]{2})\.html$ /shop/product.php?asin=$1&ukus=$2 [NC,L]

(9-digit numerical part number followed by optional "X", dash, and two-letter country code, case-insensitive, last rule)

> Should I place the rule in my main .htaccess file, or can I have a dedicated file in the /shop/ directory?

Your choice -- It's a trade-off between performance (/shop is better) vs. central administration of rules (where putting it in web root is better).



 5:41 pm on Aug 6, 2003 (gmt 0)

Thank you!

That almost works. It takes me to the product page correctly, but it can't find the product. :-(

I modified the rule slightly, and placed the htaccess file in the /shop/ ditectory. My full file is now:

<Files .htaccess>
deny from all

RewriteEngine on
RewriteRule ^([0-9]{9}).?-([a-z]{2})\.html$ /amazon1/product.php?asin=$1&ukus=$2 [NC,L]


 5:51 pm on Aug 6, 2003 (gmt 0)


Does it work now?

What character(s) does the ".?" you added match? - You stated that this was an "X" in your example URLs.



 7:39 pm on Aug 6, 2003 (gmt 0)

The code works, in that I get the product description page up, but it doesn't find the requested product.

The character can be any letter or number, afaik. The product code seems to vary. I might even need to account for differing lengths of product code, although I'm not sure. I guess a wildcard regex equivalent to the DOS * might be better than specifying the exact number and type of digits/characters. :-)

If you go to the site in my profile and append amazon1/ to the URI it you will see the kinds of codes I'm taking about. This is a testing directory; if you remove the number 1 from the end, you will see the "live" version.

Thanks again.

Red5 :-)


 8:35 pm on Aug 6, 2003 (gmt 0)


When composing regex, it's critical to describe the problem precisely. You apparently have a 9 or ten-digit numeric code, with the 9-digit codes followed by an X to pad them out to ten characters.

To rewrite only valid product numbers, try:

RewriteRule ^shop/([0-9]{9})([0-9X])-(u[sk])\.html$ /shop/product.php?asin=$1$2&ukus=$3 [NC,L]
RewriteRule ^shop.*\.html /bad_product_code_please_contact_webmaster.html [L]

To rewrite anything that looks anything like a product code:

RewriteRule ^shop/([^-]+)-([^\.]+)\.html$ /shop/product.php?asin=$1&ukus=$2 [NC,L]

The first version will rewrite to a custom error page if the product code is invalid, while the second version leaves error-handling to the called script. You can combine the techniques to make the rule more or less "accepting" of variant (or incorrect) product codes, and to redirect to a custom error page, or not, as desired.

To clarify the regex in the second rule, ([^-]+) will match all characters up to the first occurrance of "-", saving the result into local variable $1, and ([^\.]+) will match the characters following the "-", up to the first occurrance of "." and place them into $2.



 9:32 pm on Aug 6, 2003 (gmt 0)

[ Deleted for incorrect comment! ]

[edited by: Red5 at 10:11 pm (utc) on Aug. 6, 2003]


 10:02 pm on Aug 6, 2003 (gmt 0)


IT WORKS PERFECTLY! I just needed to capitalise the "ASIN".

Thank you so much for all your help. Much appreciated!


 8:28 am on Aug 7, 2003 (gmt 0)

Your mail box is full, so I can't reply. :(


 5:01 pm on Aug 8, 2003 (gmt 0)

Hello again Jim!

Right, I've done a bit of playing and had some success and I've got it running live on site (all thanks to you)!

My .htaccess file now looks like this:

RewriteEngine on
RewriteRule ^([a-z]{2})/?$ /amazon/index.php?ukus=$1 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^.]+)\.html$ /amazon/index.php?ukus=$1&SearchMode=$2&SearchKeyword=$3 [NC,L]
RewriteRule ^([^/]+)/([^\.]+)\.html$ /amazon/product.php?ASIN=$2&ukus=$1 [NC,L]

Does this look like the most efficient way of doing this to you?

It took some fiddling around to work out a couple of things, such as the fact that the order the rules are presented in is crucial (1st come, 1st serve).

Anyhow, I'd just like to thank you for the time you spent on this, and, fingers crossed, my product pages might just be spidered by Google et al now.

Kind regards,



 5:18 pm on Aug 8, 2003 (gmt 0)


It looks fine, but I would recommend not using such wild "wild-cards" if you can avoid it. Using [^/]* for every field means that one of these rules will probably match almost ANY request to this subdirectory. That may be a real problem in the future if you decide to add more variables or expand the number of countries, etc.

Personally, I would use (u[ks]) as a pattern where applicable, and require the product codes to be ten digits (as in previous posts) just to make the rules somewhat more selective.

This *is* a matter of personal preference, so you decide. Trade off efficiency, clarity, ease-of-maintenance/expansion, and robustness of the system as a whole.

> such as the fact that the order the rules are presented in is crucial
Oh, yes! Precision is the watchword with regular expressions and mod_rewrite!

I'm glad you got it working!



 12:45 pm on Aug 19, 2003 (gmt 0)

Hi again Jim,

Ok, there's something not quite right with this set up...

RewriteRule ^([a-z]{2})/?$ /amazon/index.php?ukus=$1 [NC,L]
RewriteRule ^([a-z]{2})/([^/]+)/([^.]+)\.html$ /amazon/index.php?ukus=$1&SearchMode=$2&SearchKeyword=$3 [NC,L]
RewriteRule ^([a-z]{2})/([^\.]+)\.html$ /amazon/product.php?ASIN=$2&ukus=$1 [NC,L]

All works perfectly well is I'm accessing the shop using the newly rewritten urls; the problem occurs when I access it from the old style dynamic url such as [mysite.com...]

In this case, the page is shown correctly but all of the shop links on the page have a double // in the url, ie:


The php code which generates this link outputs an anchor tag in the following form...

<a href='/amazon/$ukus/$software/english+language.html'>ESL Software</a>

which is why I'm confused as to where the additional slash is comming from.

Any ideas?


 3:06 pm on Aug 19, 2003 (gmt 0)


You've lost me... I don't know enough about what your requested vs. real URIs look like.
However, I suspect that the following Rewriterule pattern is missing a backslash to escape the period, and that may be causing trouble:

RewriteRule ^([a-z]{2})/([^/]+)/([^.]+)\.html$ /amazon/index.php?ukus=$1&SearchMode=$2&SearchKeyword=$3 [NC,L]

Also, as I stated in post 11 above, make your patterns as specific as possible in order to avoid unexpected results. If you can't tighten the patterns up any more, then add exclusions using RewriteCond.


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