Forum Moderators: phranque

Message Too Old, No Replies

Trailing slashes problem

trying to get trailing slashes working

         

colm_c

12:10 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



Hi Guys,

I've got my Mod_rewrite set up working really well band all the URLs in my site are in the form

www.mysite.com/news/2004/03/

and

www.mysite.com/about/team/membername/

etc.

The problem I'm having is that if there is no trailing slash present a 404 error is generated, which isn't the best situation at all

Because the site pretty large in size here's about a dozen or so rewrites going on.

I'm using the following rules:

RewriteRule ^news/(.*)/(.*)/ /news/archive.php?Y_ID=$1&M_ID=$2

and

RewriteRule ^about/team/(.*)/ /about/team/member.php?Name_Str=$1

and I've tried using the following rules to get the server to redirect the pages with out slashes to ones with slashes
e.g.
RewriteRule ^about/team/(.*) /about/team/$1/ [R]
RewriteRule ^about/team/(.*)/ /about/team/member.php?Name_Str=$1

And I'm just getting an infinite loop, I've tried the redirection both before and after the orginal rule and still no joy.

Anyone got any ideas on how I could solve this? without having to remove the slash idea entirely?

Any help would be greatly appreciated

Thanks

Colm

Birdman

1:08 pm on Sep 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

Welcome to Webmaster World!

I believe you can use the question mark(?) metecharacter to allow for the optional slash. Here's an example;

RewriteRule ^news/(.*)/(.*)?/ /news/archive.php?Y_ID=$1&M_ID=$2

Birdman

jdMorgan

1:32 pm on Sep 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



colm_c,

Welcome to WebmasterWorld!

Question mark should follow slash...


RewriteRule ^news/([^/]+)/([^/]*)/? /news/archive.php?Y_ID=$1&M_ID=$2
RewriteRule ^about/team/([^/]*)/? /about/team/member.php?Name_Str=$1

Do not use "(.*)" in patterns unless there is no other choice. ".*" is "greedy" and will match as many characters as possible, leading to unexpected results in many situations.

Jim

colm_c

1:57 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



Thanks for the quick replys guys

what exact does ([^/]+) mean? what kind of character restriction does it put in place?

jdMorgan

1:59 pm on Sep 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[^/]+

"One or more characters other than a slash." The surrounding parentheses then create a back-reference.

See the regular expressions tutorial cited in our forum charter for more info.

Jim

colm_c

2:05 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



I've just tried to implement that on a couple of the rewrites, but it's giving me an internal server error 500 - which is what I got before!

Do I need to add? to all my rules first? or is there some web hosting company crap going on?

SkyDog

4:22 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



I've found that .* doesn't match greedily when using mod_write and is basically equilivent to .*?

jdMorgan

4:41 pm on Sep 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are several posts here if you want to dig them up, that serve as examples to the contrary.
In addition, [^x]+x is faster in many cases, as it requires parsing left-to-right only, and no "back-offs."

Jim