Solved

Mod Rewrite causes problem with slash for directories

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wget saving web page help 4 79
PHP temp path missing 4 49
How to set default webapp for host 6 45
setup wamp server for first time 2 41
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…
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). #…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

14 Experts available now in Live!

Get 1:1 Help Now