Forum Moderators: phranque

Message Too Old, No Replies

Add trailing slash for each directory using htaccess

         

Dss_dev

12:08 pm on Nov 8, 2007 (gmt 0)

10+ Year Member



I want that when I write the url i.e http://www.example.com/dir1, it autometically add a slash at the end of dir i.e http://www.example.com/dir1/

this is not applicable only for this directory but for all the directories and subdirectories.

How it is possible using htaccess?

[edited by: encyclo at 2:24 am (utc) on Nov. 9, 2007]
[edit reason] switched to example.com [/edit]

jdMorgan

3:28 pm on Nov 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Strictly speaking, it should not be necessary for you to have to do this, since Apache mod_dir should already be doing it automatically for you. You might want to ask your host to see that mod_dir is correctly installed.

Jim

Dss_dev

5:25 am on Dec 5, 2007 (gmt 0)

10+ Year Member



Thanks for your suggestion. mod_dir Apache module is already installed in our server and we did tried all possible combinations in httpd.conf file but nothing seems to work. We also gone through the forum posts [webmasterworld.com...] and would like to know whether it is possible to add trailing slash using htaccess rules at top level? any suggestion will be greatly appreciated.

jdMorgan

1:57 pm on Dec 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a brute-force method. It is inefficient, as it adds a 'directory-exists' check to each and every trailing-slashless request to your server:

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

If you can further restrict the URL-set that must be checked (by making the rule pattern more specific or by adding additional RewriteConds ahead of the directory check), it will improve efficiency. For example, you may not need to check for directory-exists if the URL-path contains a period, or if the final part of the URL-path contains a period. Or you may be able to skip the check if the URL-path includes (or does not include) a specific directory-path. The possibilities depend on the directory layout and the format(s) of the URLs used by your site.

But be aware that the directory check invokes a call to the filesystem on your server, and that this results in the CPU running hundreds or thousands of lines of code, and possibly having to go read the disk, for each filesystem check.

Jim

Dss_dev

4:45 am on Dec 11, 2007 (gmt 0)

10+ Year Member



Thanks for your suggestion. We have tried to implement the htaccess rules which you have suggested, but unable to add a trailing slash in url of a directory. It seems the htaccess creating a endless loop in our server? Please suggest. Here goes our htaccess file structure:

###################################################
# Security #
###################################################

Options +Indexes +FollowSymlinks

<Files "errorpage.html">
Allow from all
</Files>
<Files "errorpage.php">
Allow from all
</Files>
<Files "*.css">
Allow from all
</Files>
<Files "*.gif">
Allow from all
</Files>
<Files "*.jpg">
Allow from all
</Files>

<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>

###################################################
# Don't allow .ht* files to be browsable #
# Works if AllowOverride Limit is set #
###################################################
<Files ".ht*">
deny from all
</Files>

###################################################
# Overriding php.ini #
# Works if AllowOverride Options is set #
###################################################
#php_flag error_reporting on
#php_flag register_globals on
#php_flag magic_quotes_gpc 0

###################################################
# If Rewrite module is on. #
###################################################
<IfModule mod_rewrite.c>

###################################################
# Turn the RewriteEngine on. #
###################################################
RewriteEngine on
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
rewriteCond %{HTTP_HOST} .
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
rewriteCond %{HTTP_HOST} .

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

###################################################
# Rewrite all .html to .php #
###################################################
RewriteRule ^(.*).html$ $1.php [NC]
RewriteRule ^(.*).xml$ $1.php [NC]

###################################################
# Error trapping and redirecting to custom error page #
###################################################
ErrorDocument 400 /errorpage.html?error_id=400
ErrorDocument 401 errorpage.html?error_id=401
ErrorDocument 403 /errorpage.html?error_id=403
ErrorDocument 404 /errorpage.html?error_id=404
ErrorDocument 405 /errorpage.html?error_id=405
ErrorDocument 408 /errorpage.html?error_id=408
ErrorDocument 415 /errorpage.html?error_id=415
ErrorDocument 500 /errorpage.html?error_id=500
ErrorDocument 501 /errorpage.html?error_id=501
ErrorDocument 502 /errorpage.html?error_id=502
ErrorDocument 503 /errorpage.html?error_id=503
ErrorDocument 505 /errorpage.html?error_id=505
ErrorDocument 504 /errorpage.html?error_id=504

###################################################
# General Rewriting #
###################################################

RewriteRule ^module1/admin/administration/edit-special-style modules/module1/control/editstyle.html [NC]
RewriteRule ^module1/admin/administration modules/module1/control/index.html [NC]
RewriteRule ^module1/google-sitemap modules/module1/google-sitemap-module1.xml [NC]
RewriteRule ^module1/feed modules/module1/rss.xml [NC]
RewriteRule ^module1/archive/(.*)?$ modules/module1/articlearchive.html [NC]
RewriteRule ^module1/(editŠadd) modules/module1/module1_add_edit.html [NC]
RewriteRule ^module1/(.*)\.html modules/module1/module1.html [NC]
RewriteRule ^module1/(.*)?$ modules/module1/index.html [NC]
RewriteRule ^module1(/)?$ modules/module1/ [NC]

</IfModule>