Forum Moderators: phranque

Message Too Old, No Replies

rewrite product directory to script

         

TristanToxic

12:07 pm on Oct 24, 2006 (gmt 0)

10+ Year Member



Hi,

I have product pages on my site like this:

<site.com>/producta/
and this I want to rewrite to
<site.com>/cgi-bin/product.pl?type=producta

ofcourse since the products aren't in a subdirectory I need to apply this to everything, but I still want to be able to access for example:
<site.com>/statistics/

would this in my .conf suffice?

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI}!statistics$
RewriteRule ^([^./]+)/?$ /cgi-bin/product.pl?type=$1 [L]

Thanks

jdMorgan

1:16 pm on Oct 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, you'll need to remove the end-anchor "$" from the "^/statistics$" pattern. Otherwise, only exactly "/statistics" will match.

You may also wish to conmsider whether you wish to rewrite all common support files like robots.txt, compact provacy policy, content-rating, and custom error document files to your script. If not, these paths should also be excluded.

The two usual methods are to use an exclusionary RewriteCond as you have done, or to check the %{REQUEST_FILENAME} to see if a 'real' file or directory of that name exists. Then rewrite to the script only if a real file or directory of that name does not exist. The former method is much more efficient, but the latter may be easier to maintain on large sites with lots of static pages.

Jim

TristanToxic

1:53 pm on Oct 24, 2006 (gmt 0)

10+ Year Member



thanks for the reply!

wouldn't the [^./] pattern exclude any files with a dot in it like robots.txt etc?
All of our files have a dot in them, and there are (except for some exceptions) no subdirectories in our root.
Would using my proposed solution be the most efficient and not cause any internal apache redirect loops or so?

thanks!

jdMorgan

2:27 pm on Oct 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes -- actually, I didn't spot that [^.] bit until after I posted. Your rule will exclude any URL with a filetype as-is, and no further exclusions are needed.

No potential 'loops' are evident, since you're rewriting to a path containing both a filetype (i.e. containing a period) AND a subdirectory (i.e. containing a slash), so the substitution URL will be excluded by the RewriteRule pattern, precluding a loop.

Jim

[edited by: jdMorgan at 2:36 pm (utc) on Oct. 24, 2006]

TristanToxic

2:34 pm on Oct 24, 2006 (gmt 0)

10+ Year Member



thanks, I'll buy you a beer if I meet you irl :-)

jdMorgan

2:36 pm on Oct 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, but I type poorly enough when sober!

Jim