Page 1 of 1

Mod ReWrite Issue. Trailing Slash Causing Issues

Posted: Tue Sep 15, 2009 1:09 pm
by jbh
Hey,

I needed to take a dynamic url, such as

visit.php?user=jimmy&prod_id=5

and turn it into:

http://www.domain.com/jimmy/5

This allows sales merchants to promote a product that we can track referrals for without a clunky url with querystrings/etc.
Well, I was able to do that. The only problem, though, is that if the '5' at the end of the url has a trailing slash, such as:

http://www.domain.com/jimmy/5/ --> the '404 not found page' loads up instead of redirecting to the merchant url.

Without the trailing slash, it works fine. I have no complaints UNLESS it is a / ending in the url.

How do I allow the following code in my .htaccess to allow a trailing slash without giving the 404 page errors?

Code: Select all

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(exe|css|js|jpeg|jpg|gif|html|php|ttf|png)$
RewriteRule ^([^/]+)/([^/]+)$ /visit.php?prod_id=$1&aff_id=$2&page=$3
ErrorDocument 404 /404.php
 
Thank you very much for your time.

Re: Mod ReWrite Issue. Trailing Slash Causing Issues

Posted: Tue Sep 15, 2009 4:39 pm
by onion2k
Add an optional slash at the end of the expression with \/?

(I'd post a more verbose example but I'm on my iPhone at the moment.)

Re: Mod ReWrite Issue. Trailing Slash Causing Issues

Posted: Wed Sep 16, 2009 10:37 am
by ridgerunner
jbh wrote:Hey,

I needed to take a dynamic url, such as

visit.php?user=jimmy&prod_id=5

and turn it into:

http://www.domain.com/jimmy/5

This allows sales merchants to promote a product that we can track referrals for without a clunky url with querystrings/etc.
Well, I was able to do that. The only problem, though, is that if the '5' at the end of the url has a trailing slash, such as:

http://www.domain.com/jimmy/5/ --> the '404 not found page' loads up instead of redirecting to the merchant url.

Without the trailing slash, it works fine. I have no complaints UNLESS it is a / ending in the url.

How do I allow the following code in my .htaccess to allow a trailing slash without giving the 404 page errors?

Code: Select all

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(exe|css|js|jpeg|jpg|gif|html|php|ttf|png)$
RewriteRule ^([^/]+)/([^/]+)$ /visit.php?prod_id=$1&aff_id=$2&page=$3
ErrorDocument 404 /404.php
 
Thank you very much for your time.
Yes, onion2k is correct: you simply need to add an optional / to the end of the RewriteRule regex (i.e. "^([^/]+)/([^/]+)/?$"). Also, there is a problem with your rule in that the replacement string uses three backreferences ($1, $2 and $3), but your regex has only two capturing parentheses. And the query variables in your rewrite regex code do not correspond the examples you set forth earlier in the body of your message. That said, here is a version that may do the trick:

Code: Select all

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(exe|css|js|jpeg|jpg|gif|html|php|ttf|png)$
RewriteRule ^([^/]+)/([^/]+)/?$ /visit.php?user=$1&prod_id=$2
ErrorDocument 404 /404.php

Re: Mod ReWrite Issue. Trailing Slash Causing Issues

Posted: Wed Sep 16, 2009 1:20 pm
by jbh
Thank you, both. Your solution has worked perfectly. Regex is one thing I just struggle with
but I am going to make sure I sit down, first chance, and really bone up on it.

Have a great day to both of you.