Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Apache: Users

mod_rewrite to fix trailing slash problem

 

 

Apache users RSS feed   Index | Next | Previous | View Threaded


tjg at soe

May 15, 2008, 8:59 AM

Post #1 of 4 (332 views)
Permalink
mod_rewrite to fix trailing slash problem

Hi,

I have a web page that works when you include the trailing slash:

http://www.foo.com/blah/

But, if you leave the trailing slash off, it does not work and you get a 403
error:

http://www.foo/com/blah

So, I tried to fix this using some mod_rewrite rules, as follows:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1/ [R,L]

But it seems that the "-d" test is always failing. If I comment that part
out, then *everything* gets a "/" appended to the end, which of course
doesn't work. I tried using "!-f" instead, and got similarly bad results.

I've Googled this and read lots of solutions that all look more or less like
what I've posted above, but none of them seem to actually work.

Is there something I'm missing here?

FWIW, I'm running Apache 2.2 on CentOS 5.1.

Thanks for any help you can give!

Tim Gustafson
SOE Webmaster
UC Santa Cruz
tjg [at] soe
(831) 459-5354



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe [at] httpd
" from the digest: users-digest-unsubscribe [at] httpd
For additional commands, e-mail: users-help [at] httpd


ebowman at boboco

May 15, 2008, 9:29 AM

Post #2 of 4 (302 views)
Permalink
Re: mod_rewrite to fix trailing slash problem [In reply to]

Tim Gustafson wrote:
> Hi,
>
> I have a web page that works when you include the trailing slash:
>
> http://www.foo.com/blah/
>
> But, if you leave the trailing slash off, it does not work and you get a 403
> error:
>
> http://www.foo/com/blah
>
> So, I tried to fix this using some mod_rewrite rules, as follows:
>
> RewriteEngine On
> RewriteCond %{REQUEST_FILENAME} -d
> RewriteCond %{REQUEST_URI} !/$
> RewriteRule (.*) $1/ [R,L]
>
> But it seems that the "-d" test is always failing. If I comment that part
> out, then *everything* gets a "/" appended to the end, which of course
> doesn't work. I tried using "!-f" instead, and got similarly bad results.
>
> I've Googled this and read lots of solutions that all look more or less like
> what I've posted above, but none of them seem to actually work.
>
> Is there something I'm missing here

It's a bad idea to rewrite in this case, since it can cause all kinds of
problems with relative URLs.

Better to redirect:

Redirect permanent /blah http://www.foo.com/blah/

cheers,
Eric

--
Eric Bowman
Boboco Ltd
ebowman [at] boboco
http://www.boboco.ie/ebowman/pubkey.pgp
+35318394189/+353872801532


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe [at] httpd
" from the digest: users-digest-unsubscribe [at] httpd
For additional commands, e-mail: users-help [at] httpd


tjg at soe

May 15, 2008, 11:37 AM

Post #3 of 4 (302 views)
Permalink
RE: mod_rewrite to fix trailing slash problem [In reply to]

Well, the problem is that this applies to thousands of URLs. Generally,
Apache does the redirection for you, but I've read somewhere that sometimes
Apache's redirection can be broken by other mod_rewrite rules. I've
inherited this system, and the config files are thousands of lines long, so
I haven't had time to analyze all the scenarios yet. But I do know that
this wasn't a problem on Apache 1.3, and when I upgraded the system to
Apache 2.2, links to folders without trailing slashes broke. :\

Tim Gustafson
SOE Webmaster
UC Santa Cruz
tjg [at] soe
(831) 459-5354


-----Original Message-----
From: Eric Bowman [mailto:ebowman [at] boboco]
Sent: Thursday, May 15, 2008 9:30 AM
To: users [at] httpd
Subject: Re: [users [at] http] mod_rewrite to fix trailing slash problem

Tim Gustafson wrote:
> Hi,
>
> I have a web page that works when you include the trailing slash:
>
> http://www.foo.com/blah/
>
> But, if you leave the trailing slash off, it does not work and you get a
403
> error:
>
> http://www.foo/com/blah
>
> So, I tried to fix this using some mod_rewrite rules, as follows:
>
> RewriteEngine On
> RewriteCond %{REQUEST_FILENAME} -d
> RewriteCond %{REQUEST_URI} !/$
> RewriteRule (.*) $1/ [R,L]
>
> But it seems that the "-d" test is always failing. If I comment that part
> out, then *everything* gets a "/" appended to the end, which of course
> doesn't work. I tried using "!-f" instead, and got similarly bad results.
>
> I've Googled this and read lots of solutions that all look more or less
like
> what I've posted above, but none of them seem to actually work.
>
> Is there something I'm missing here

It's a bad idea to rewrite in this case, since it can cause all kinds of
problems with relative URLs.

Better to redirect:

Redirect permanent /blah http://www.foo.com/blah/

cheers,
Eric

--
Eric Bowman
Boboco Ltd
ebowman [at] boboco
http://www.boboco.ie/ebowman/pubkey.pgp
+35318394189/+353872801532


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe [at] httpd
" from the digest: users-digest-unsubscribe [at] httpd
For additional commands, e-mail: users-help [at] httpd


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe [at] httpd
" from the digest: users-digest-unsubscribe [at] httpd
For additional commands, e-mail: users-help [at] httpd


fhuddles at yahoo

May 15, 2008, 4:25 PM

Post #4 of 4 (312 views)
Permalink
Re: mod_rewrite to fix trailing slash problem [In reply to]

You've probably already seen this, but just in case: I see in the config
file in the "Alias" section, this:
# Alias: Maps web paths into filesystem paths and is used to
# access content that does not live under the DocumentRoot.
# Example:
# Alias /webpath /full/filesystem/path
#
# If you include a trailing / on /webpath then the server will
# require it to be present in the URL. You will also likely
# need to provide a <Directory> section to allow access to
# the filesystem path.

This web page, or these web pages, wouldn't happen to be directed via
aliases with trailing slashes, would they? Looks as though that would
cause the symptom you
are describing. I doubt this is new behaviour with 2.2, though.

Frank Huddleston

Tim Gustafson wrote:
> Hi,
>
> I have a web page that works when you include the trailing slash:
>
> http://www.foo.com/blah/
>
> But, if you leave the trailing slash off, it does not work and you get a 403
> error:
>
> http://www.foo/com/blah
>
> So, I tried to fix this using some mod_rewrite rules, as follows:
>
> RewriteEngine On
> RewriteCond %{REQUEST_FILENAME} -d
> RewriteCond %{REQUEST_URI} !/$
> RewriteRule (.*) $1/ [R,L]
>
> But it seems that the "-d" test is always failing. If I comment that part
> out, then *everything* gets a "/" appended to the end, which of course
> doesn't work. I tried using "!-f" instead, and got similarly bad results.
>
> I've Googled this and read lots of solutions that all look more or less like
> what I've posted above, but none of them seem to actually work.
>
> Is there something I'm missing here?
>
> FWIW, I'm running Apache 2.2 on CentOS 5.1.
>
> Thanks for any help you can give!
>
> Tim Gustafson
> SOE Webmaster
> UC Santa Cruz
> tjg [at] soe
> (831) 459-5354
>
>
>
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe [at] httpd
> " from the digest: users-digest-unsubscribe [at] httpd
> For additional commands, e-mail: users-help [at] httpd
>
>
>

Apache users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.