Forum Moderators: phranque

Message Too Old, No Replies

rewrite url folder

         

sampford

11:40 pm on Feb 21, 2007 (gmt 0)

10+ Year Member



Is it possible to rewrite all url folders to include a forward slash at the end?

I ask, because for some strange reason, www.domain.tld/folder - does not work, yet www.domain.tld/folder/ will work. There is a possibility that there could be numerous folders to go through.

Is this best covered by using .htaccess, or is there something else I could try?

sampford

11:52 pm on Feb 21, 2007 (gmt 0)

10+ Year Member



Found this eleswhere - it may help others having a similar problem.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_URI}!(.*)/$
RewriteRule ^(.*)$ [domain.com...] [L,R=301]

If there is a better solution, please let me know - thanks!

jdMorgan

12:01 am on Feb 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



According to the rules, www.example.com/folder is a file, and www.example.com/folder/ is the "index" of the "/folder" directory of www.example.com, which might be either a server-generated directory listing or a page, depending on how you've set things up.

On Apache, mod_dir [httpd.apache.org] generally does a fix-up on missing trailing slashes for directories, if no such page exists. In other words, if the page "/folder" does not exist, mod_dir will append a slash and check if it exists as a directory. If so, then it redirects to that URL.

If for some reason you cannot get mod_dir installed or working, the same thing can be done with mod_rewrite, although mod_dir is much more efficient:


# If requested filename does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# but does exist as a directory when a slash is appended
RewriteCond %{REQUEST_FILENAME}/ -d
# then redirect any URL-path without a slash or "." in the final path-part, appending a slash
RewriteRule ^(([^/]+/)*[^/.]+)$ http://www.example.com/$1/ [R=301,L]

This code is intended for use in .htaccess as written. I also left out the mod_rewrite "setup" assuming you've already got some working mod_rewrite rules.

Jim

[edited by: jdMorgan at 12:02 am (utc) on Feb. 22, 2007]