Forum Moderators: phranque

Message Too Old, No Replies

Rewrite problem on sub-directory

mod rewrite override for directories

         

nvisioncurtis

4:01 pm on Mar 21, 2011 (gmt 0)

10+ Year Member



Hi All,

Hoping someone can advise on how to adjust the mod-rewrite rules below to allow for normal URL function if an actual directory or file exist?

Currently, /controlpanel/ creates an infinite redirect loop. whereas /controlpanel/index.php works correctly.

Otherwise these rules appear to work correctly.

ex. /controlpanel/index.php?p=login, or /controlpanel/ would function as if there are no rewrite rules because and use default to the index.php within that directory.


Below is my complete .htaccess file


Options +FollowSymLinks
RewriteEngine On
#RewriteBase /orientation_dev
RewriteBase /

#if the file exist pass it on thru and exit.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+) $1 [L]

#if the file NOT a directory redirect to index
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ index.php?q=$1 [L]
RewriteRule ^(.+)$ $1/ [L,R=301]

nvisioncurtis

6:38 pm on Mar 21, 2011 (gmt 0)

10+ Year Member



I found a solution. I don't fully understand the difference but it seems to be working for me.

I post here for others.

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L,NE]

# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?q=$1 [QSA,NE]

g1smd

6:47 pm on Mar 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For pages it is better if the URL doesn't end with a trailing slash. Trailing slash indicates a folder or the index page of a folder.

The -f and -d "exists" checks are very slow and disk intensive and they are best avoided by using more specific RegEx patterns and by using a preceding RewriteCond excluding all requests that will never be rewritten and which therefore don't need to be -f and -d checked.

The redirect needs the protocol and domain name added to the rule target.

The rewrite needs the [L] flag added.