Forum Moderators: phranque
I've got my site set up with a custom shop, using product pages of the form:
/shop/020530902X-us.html
I'm now trying to write a RewriteRule to convert requests for the above page to:
/shop/product.php?asin=020530902X&ukus=us
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 :-)
> 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]
> 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).
HTH,
Jim
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
</Files>
RewriteEngine on
RewriteRule ^([0-9]{9}).?-([a-z]{2})\.html$ /amazon1/product.php?asin=$1&ukus=$2 [NC,L]
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 :-)
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]
RewriteRule ^shop/([^-]+)-([^\.]+)\.html$ /shop/product.php?asin=$1&ukus=$2 [NC,L]
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.
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,
Red5
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!
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:
[mysite.com...]
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?
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.
HTH,
Jim