Welcome to WebmasterWorld Guest from 18.232.124.77

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Removing trailing slashes on files that don't exist?

but leaving them on directories

     
8:30 pm on Jun 17, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 18, 2008
posts: 73
votes: 0


Greetings,
Here is my dilemma:

I currently have htaccess setup to add trailing slashes onto directories:

test becomes test/

However, I am trying to 301 some non-existent pages that are still in SE cache. For example:

board/6040.php does no longer exist on our site, so I can't use the !-d check for the rewrite. So when I try to redirect, I end up with a 404 of board/6040.php/

Any suggestions on how to remove that slash?

Thanks

8:56 pm on June 17, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


It would be helpful if you posted your code.

Also, your title says, "trailing spaces" -- Do you mean trailing slashes?

Jim

9:04 pm on June 17, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 18, 2008
posts:73
votes: 0


Yes! sorry. I will get the code up in a bit.
9:13 pm on June 17, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


Can you test for the period in the filename and not redirect for those?
3:15 pm on June 18, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 18, 2008
posts: 73
votes: 0


Hey all,
Here is the code I am currently using. Be gentle, I am still quite the n00b. Thanks!

RewriteEngine on
# Use PHP5 as default
AddHandler application/x-httpd-php5 .php
errordocument 404 /error.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]

[edited by: jdMorgan at 5:12 pm (utc) on June 18, 2009]
[edit reason] example.com [/edit]

5:14 pm on June 18, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


I'd suggest:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ http://example.com/$1/ [R=301,L]

The rule pattern matches only URLs at any subdirectory level which do not end in a slash or contain a period in the final path-part.

Jim

5:29 pm on June 18, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 18, 2008
posts: 73
votes: 0


Thanks Jim,
Works great! Sorry for the URL in there. Overlooked it when I pasted.
12:22 pm on June 19, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 23, 2003
posts:915
votes: 0


I currently have htaccess setup to add trailing slashes onto directories:

test becomes test/

Doesn't Apache do this automagically, even without .htaccess in place?

On my webservers, requesting http://www.example.com/directory gets you a 301 redirect to http://www.example.com/directory/ (assuming 'directory' actually exists...)

No need for htaccess or mod_rewrite at all (?)

12:28 pm on June 19, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 18, 2008
posts: 73
votes: 0


I would have thought it myself, but no, it wouldn't add them until I changed the htaccess file.
12:41 am on June 20, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


You might also want to look at the
DirectorySlashes
directive.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members