?
Solved

Mod Rewrite causes problem with slash for directories

Posted on 2008-10-03
5
Medium Priority
?
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 27

Accepted Solution

by:
caterham_www earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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