I've got a basic URL rewrite that works well, except when navigating to folders:
When a user navigates to any folder mydomain.com/folder/he is redirected to mydomain.com/folder?pl1=css, causing an infinite redirect loop.
I've tried adding RewriteCond %{REQUEST_FILENAME}/ -d just above the rule that redirects pages to their versions without trailing slashes. This solves the infinite loop problem, but breaks the redirecting to the pages without trailing slashes (which I would like to keep for SEO reasons: http://googlewebmastercentral.blogspot.be/2010/04/to-slash-or-not-to-slash.html
My question:
- How to handle folders correclty => show default page (/folder/index.html; if it exists) within the folder when navigating to
mydomain.com/folderormydomain.com/folder/(without appending variables to url) - For extra brownie points: how to optimize the second part of the rewrite, so as not to use 6 lines of code :-)
This is my code:
# Start the rewrite engine
<IfModule mod_rewrite.c>
Options +FollowSymlinks
Options -MultiViews
RewriteEngine On
</IfModule>
# Remove trailing slash
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Rule below fixes loop, but breaks redirection
# RewriteCond %{REQUEST_FILENAME}/ -d
# Handle my GET variables
RewriteRule ^([A-Za-z0-9-_]+)/?$ index.php?pl1=$1 [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ index.php?pl1=$1&pl2=$2 [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ index.php?pl1=$1&pl2=$2&pl3=$3 [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ index.php?pl1=$1&pl2=$2&pl3=$3&pl4=$4 [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ index.php?pl1=$1&pl2=$2&pl3=$3&pl4=$4&pl5=$5 [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ index.php?pl1=$1&pl2=$2&pl3=$3&pl4=$4&pl5=$5&pl6=$6 [L]