Matt Q
Matt Q

Reputation: 1

htaccess weird trailing slash problem

url: http://localhost/url/of/the/keyword/whatever/

RewriteRule ^url/of/the/keyword/([a-z]+)/?$ ?keyword=$1 [L]  
// php  
echo $_GET['keyword'];    
// outputs **whatever** (OK)
RewriteRule ^url/of/the/keyword/(.*)/?$ ?keyword=$1 [L]  
// php  
echo $_GET['keyword'];  
// outputs **whatever/** (with a trailing slash, which is not expected)

can anyone explain why there's a trailing slash for the second condition?


Also, how can I allow percentage sign in url rewrite?

http://localhost/url/of/the/keyword/%40%23%24/

RewriteRule ^url/of/the/keyword/([0-9a-zA-Z-.%])/?$ ?keyword=$1 [L]

the above rule doesn't work. can anyone correct this so it allows a-Z, 0-9, dot, hyphen, and percentage sign?

Thanks!

Upvotes: 0

Views: 1003

Answers (1)

Jason McCreary
Jason McCreary

Reputation: 72971

You are getting the / for the second RewriteRule because .* is greedy. That is to say it greedily captures the trailing slash because you've marked it as optional /?. It's best to be specific with your patterns (like the first RewriteRule) to avoid such situations.

The pattern you match can accept anything. Just remember it has to be a valid URL. The problem is you forgot the quantifier. So you're only matching one character from the grouping.

Add the +

RewriteRule ^url/of/the/keyword/([0-9a-zA-Z\-.%]+)/?$ ?keyword=$1 [L]

Upvotes: 2

Related Questions