Forum Moderators: phranque

Message Too Old, No Replies

rewrite for product pages and product details

         

TristanToxic

12:35 pm on Dec 21, 2006 (gmt 0)

10+ Year Member



I'm currently rewriting all my product pages like this:

www-site-com/widget/ to www-site-com/cgi-bin/product?name=widget

with this in my httpd.conf:

RewriteCond %{REQUEST_URI}!stats
RewriteCond %{REQUEST_URI}!banners
RewriteRule ^([^./]+)/?$ /cgi-bin/product?name=$1 [L]

the problem is now that both:
www-site-com/widget
www-site-com/widget/
work (which is ok), but they should both go to:
www-site-com/widget/

as an extra I would also want to have a picture gallery of a product rewrite like this:

www-site-com/widget/pictures/
(and again that both /pictures and /pictures/ work but both go to /pictures/)

ofcourse the "pictures" is optional.

I thought of something like this:

RewriteRule ^([^./]+)/?([^./]+)?/?$ /cgi-bin/product?name=$1&page=$2 [L]

but I thought to show it here first to avoid breaking my site

Tristan

jd01

7:23 pm on Dec 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you are looking for something like the following if all you would really like to do is add the trailing / to requests.

RewriteCond %{REQUEST_URI} !(\.¦/$)
RewriteRule . http://www.example.com%{REQUEST_URI}/ [R=301,L]

Rule: If there is a single char. (or more) requested, check the condition.

Cond: If the requested URI does not contain a (dot) or end in a / continue the rule.

Rule: If the Cond. was met, redirect the request to the original location 'with a slash'.

Justin

TristanToxic

3:33 pm on Dec 25, 2006 (gmt 0)

10+ Year Member



Hi,

thanks! I've changed my httpd.conf for this domain to this:

----
RewriteCond %{REQUEST_URI}!(\.¦/$)
RewriteRule . %{REQUEST_URI}/ [R=301,L]

RewriteCond %{REQUEST_URI}!^stats$
RewriteRule ^([^./]+)/([^./]+)/$ /cgi-bin/product?name=$1&page=$2 [L]

RewriteCond %{REQUEST_URI}!^stats$
RewriteRule ^([^./]+)/?$ /cgi-bin/product?name=$1&page=main [L]
----

which adds the end-/ to pages ( www-site-com/widget/ ) and allows for a product "mainpage" and detail pages

This seems to work all okay.

TristanToxic

5:34 pm on Jan 1, 2007 (gmt 0)

10+ Year Member



Apparently there must be something wrong with my rewrites and a normal product "page" www-site-com/product/ loads the underlying script twice. I checked this by adding a counter in the script that increments a database value with 1. Each time I reload the page the counter seems to have increased by 2...

these are the rewrites I currently use:

----
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(s?html?¦php)\ HTTP/
RewriteRule ^(.*)index\.(s?html?¦php) [www-site-com...] [R=301,L]

RewriteCond %{REQUEST_URI}!(\.¦/$)
RewriteRule . %{REQUEST_URI}/ [R=301,L]

RewriteCond %{REQUEST_URI}!stats
RewriteRule ^([^./]+)/$ /cgi-bin/product?name=$1&page=main [L]

RewriteCond %{REQUEST_URI}!stats
RewriteRule ^([^./]+)/([^./]+)/$ /cgi-bin/product?name=$1&page=$2 [L]
----

so www-site-com/widget/ should load the underlying script with the "widget" product and "main" page; while for example www-site-com/widget/detail/ should load the underlying script with the "widget" product and "detail" page

Can anyone spot an error in the rewriteconditions or the order of them?

Thanks!

jdMorgan

8:39 pm on Jan 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mod_rewrite is incapable of loading the same page twice per HTTP request, because mod_rewrite only works in the URL-to-filename translation phase of the Apache API. Once a content-handler (like your script) is invoked in the content-handling phase, control never returns to mod_rewrite.

I would suggest using a server headers checker to determine if you script is generating an external redirect response, which would cause the client to make a second request using the URL provided in that redirect response.

I use the "Live HTTP Headers" extension to the Firefox browser for this kind of testing -- very handy.

Jim