Solved

Mod Rewrite causes problem with slash for directories

Posted on 2008-10-03
5
295 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now