1

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/folderor mydomain.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] 
0

1 Answer 1

1

If this "...except when navigating to folders..." means to existing folders, you can try adding the next 3 lines before the comment # Remove trailing slash:

RewriteCond %{REQUEST_FILENAME} -f  [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .*   - [L]

It is possible to reduce the rules to a single one, as long as there is no problem having empty keys (pl6=). Like this:

# Handle my GET variables
RewriteCond %{REQUEST_URI}  !index\.php
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,NC]

Makes all parameters optional, except the first one.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks so muchfor your answer! If I implement the first part of your answer I get the desired effect, except that it redirects to ´/folder/?pl1=folder´. Any ideas? The second part of your answer does not seem to work, as it generates 404 errors in my system. Also, I do mind to have empty keys.
"Also, I do mind to have empty keys". Then, you need a rule for each parameter, as it is in your question.
"...redirects to ´/folder/?pl1=folder" I guess it is redirected by another rule that adds the query. You may try deleting RewriteCond %{REQUEST_FILENAME} -f [OR] in my answer so it doesn't look for files. But not sure of the result. Make sure the browser's cache is cleared before any test.
The winner takes it all! You solved my question. Thank you for answering every aspect of my question in such a clear and useful fashion! Good call on clearing cache aswel.
Sure did. I guess I will keep my uncool 6 lines of code for my variable rewrites though ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.