Mod ReWrite Issue. Trailing Slash Causing Issues

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
jbh
Forum Commoner
Posts: 89
Joined: Tue Dec 05, 2006 7:01 pm

Mod ReWrite Issue. Trailing Slash Causing Issues

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Mod ReWrite Issue. Trailing Slash Causing Issues

Post 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.)
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Mod ReWrite Issue. Trailing Slash Causing Issues

Post 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
jbh
Forum Commoner
Posts: 89
Joined: Tue Dec 05, 2006 7:01 pm

Re: Mod ReWrite Issue. Trailing Slash Causing Issues

Post 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.
Post Reply