Solved

Mod Rewrite causes problem with slash for directories

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
500 Internal Server Error When I Run Code Igniter 4 80
Changing Sitemap Frequency on XML File 3 79
PHP Error Settings 12 86
htaccess restrict subdomain 4 141
Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

696 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