Solved

Mod Rewrite causes problem with slash for directories

Posted on 2008-10-03
5
299 Views
Last Modified: 2009-01-23
I use the Mod Rewrite Engine to direct all queries that are not to an exsiting file or directory to index.php.

The problem is that now if someone forgets to add a slash to a directory they get a 404 error.

If a user uses this URI:
http://www.example.com/dir

If /dir/ is a directory that exsists then the visitor should AUTOMATICLALY be directed to
http://www.example.com/dir/

But now the slash is not automatically added.

php_flag display_errors on
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_URI} !^/cgi/
RewriteRule ^(.*) index.php [L]

Open in new window

0
Comment
Question by:hankknight
  • 2
  • 2
5 Comments
 
LVL 27

Accepted Solution

by:
caterham_www earned 500 total points
ID: 22633380
You'll need an extra rule to fix a missing trailing slash

> If /dir/ is a directory that exsists

Hmmm with - or w/o slash -- RewriteCond %{REQUEST_FILENAME} !-d should usually skip the rule if the directory exists.
php_flag display_errors on
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/$
RewriteRule !\.[a-z]{2,4}$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/cgi/
RewriteRule ^ index.php [L]

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 22633549
Your code does NOT help.  

When I go here:
http://www.example.com/dir

I still do NOT get directed to here:
http://www.example.com/dir/
0
 
LVL 2

Expert Comment

by:storeytime
ID: 22635279
do you have mod_dir?

Use Directory index it will automatically redirect if someone forgets trailing slash

http://httpd.apache.org/docs/1.3/mod/mod_dir.html#directoryindex
0
 
LVL 16

Author Comment

by:hankknight
ID: 22635756
I don't use mod_dir. Can't this be done with just mod rewrite?
0
 
LVL 2

Expert Comment

by:storeytime
ID: 22636819
it can but I am lazy and like the simplest solution aka no code to write or manage I only use rewrites to force https
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question