Page 1 of 1

mod_rewrite -> problems with optional / character

Posted: Fri Apr 28, 2006 3:22 pm
by visionmaster
Hello together,

Entering http://www.webpage.de/products Apache redirects to the correct webpage http://www.webpage.de/products/maschine ... enbau.html

Entering http://www.webpage.de/products/ (notice the last / character) displays following error message:

File Not Found
The requested URL /products/ was not found on this server.

Following rule works out fine, http://www.webpage.de/products is displayed properly:

.htaccess:
-------------

Code: Select all

RewriteEngine on
RewriteRule ^.*/(.+)\.html$ 
RewriteRule /products$ http://www.webpage.de/products/maschinenbau/maschinenbau.html

Extending the RewriteRule with the optional character / at the end of products strangely brings the "File Not Found" error message for both cases:

http://www.webpage.de/products
and
http://www.webpage.de/products/

.htaccess incorrect:
------------------------

Code: Select all

RewriteEngine on
RewriteRule ^.*/(.+)\.html$ 
RewriteRule /produkte/?$ http://www.webpage.de/products/maschinenbau/maschinenbau.html

=> What am I doing wrong? Why can't I add the optional / at the end?


Thanks,
visionmaster

Re: mod_rewrite -> problems with optional / character

Posted: Sun Apr 30, 2006 1:05 am
by visionmaster
Hello together,

I solved my problem as follows:

Code: Select all

RewriteEngine On
RewriteRule ^products/?$  /modules/mod_search.php?action=startSearch&suche[searchstring]=maschinenbau&suche[results]=10&suche[bundesland]=18&modus=all&seo=on
RewriteRule ^products/.+/(.+)\.html$ /modules/mod_search.php?action=startSearch&suche[searchstring]=$1&suche[results]=10&suche[bundesland]=18&modus=all&seo=on

Here ist the webpage that brought me on the right track: http://www.yourhtmlsource.com/sitemanag ... iting.html :
If your site visitor had entered something like products/12, the rule above won’t do a redirect, as the slash at the end is missing. To promote good URL writing, we’ll take care of this by doing a direct redirect to the same URL with the slash appended.

RewriteRule ^products/([0-9][0-9])$ products/$1/ [R]

Multiple redirects in the same .htaccess file can be applied in sequence, which is what we’re doing here. This rule is added before the one we did above, like so:

RewriteRule ^products/([0-9][0-9])$ products/$1/ [R]
RewriteRule ^products/([0-9][0-9])/$ productinfo.php?prodID=$1

Thus, if the user types in the URL products/12, our first rule kicks in, rewriting the URL to include the trailing slash, and doing a new request for products/12/ so the user can see that we likes our trailing slashes around here. Then the second rule has something to match, and transparently redirects this URL to productinfo.php?prodID=12.