Forum Moderators: phranque

Message Too Old, No Replies

Removing trailing slashes on files that don't exist?

but leaving them on directories

         

dpinion

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

10+ Year Member



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

jdMorgan

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

WebmasterWorld Senior Member 10+ Year Member



It would be helpful if you posted your code.

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

Jim

dpinion

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

10+ Year Member



Yes! sorry. I will get the code up in a bit.

g1smd

9:13 pm on Jun 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you test for the period in the filename and not redirect for those?

dpinion

3:15 pm on Jun 18, 2009 (gmt 0)

10+ Year Member



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]

jdMorgan

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

WebmasterWorld Senior Member 10+ Year Member



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

dpinion

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

10+ Year Member



Thanks Jim,
Works great! Sorry for the URL in there. Overlooked it when I pasted.

webdoctor

12:22 pm on Jun 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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 (?)

dpinion

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

10+ Year Member



I would have thought it myself, but no, it wouldn't add them until I changed the htaccess file.

g1smd

12:41 am on Jun 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You might also want to look at the
DirectorySlashes
directive.