Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let me start immediately with my problem:

Options +FollowSymLinks
RewriteEngine On

RewriteBase / 
RewriteRule ^([a-zA-Z0-9_-]+)$$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$$1&var2=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$$1&var2=$2&var3=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$$1&var2=$2&var3=$3&var4=$4 [L]

RewriteRule ^([a-zA-Z0-9_-]+)/$$1 [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$$1/$2 [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$$1/$2/$3 [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$$1/$2/$3/$4 [R=301,L]

There is folder and when I try to access http://localhost/ I am redirected to


What I would like is when user enters http://localhost/ that he is redirected to http://localhost/ I tried to do this with header("Location:... ") but it always redirects me to http://localhost/ If folder administracija is renamed then header() function works. Any ideas how to solve this?

Thanks, Ile

share|improve this question
What's in your httpd.conf file? – random Oct 12 '09 at 10:12
up vote 1 down vote accepted

Testing on my own Apache configuration I created the directory DOCUMENT_ROOT/ and placed the .htaccess file in there along with a simple index.php file that just dumped the $_REQUEST contents back to me.

Here is the .htaccess I used:

Options +FollowSymLinks
RewriteEngine On

RewriteBase /
RewriteCond %{REQUEST_URI} !^/
RewriteRule ^([^/]+)$ index.php?var1=$1 [R,L,QSA]
RewriteRule ^([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2 [R,L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2&var3=$3 [R,L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?var1=$1&var2=$2&var3=$3&var4=$4 [R,L,QSA]

The RewriteCond line catches the index.php to keep it from going into a recursive loop. I also gave each RewriteRule the redirect (R) and query string append (QSA) flags in addition to tell them they were the last (L) rule to apply.

I also changed the match grouping to include anything except a slash (/) to simplify the matching.

share|improve this answer
Thank you very much for answering! – ile Oct 15 '09 at 11:38
No problem... I've had my own go-rounds with mod_rewrite... it can be a tricky bastard to nail down sometimes. – Jeremy Bouse Oct 15 '09 at 12:48

Hmmmm...what about trying the


directive in the .htaccess file under the administracija folder? In this case, I'd specifically suggest:

DirectoryIndex index.php
share|improve this answer
It doesn't work. Only when I remove last 4 lines from htaccess then it works... actually I'm still redirected to the same address but this time content is displayed. Also, I don't know how users are going to reach forum because this is going to existing portal and users are used to reach content via specific url which is now not accessible. – ile Oct 12 '09 at 10:34
I found solution on stackoverflow: RewriteCond %{REQUEST_FILENAME}/index.php -f RewriteRule ^ %{REQUEST_URI}/index.php And it must be places before last 4 lines in above example – ile Oct 12 '09 at 10:50

couldnt get text being displayed as code in comment so I'll have to use answer...

RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^ %{REQUEST_URI}/index.php
share|improve this answer

The following rule is causing the behavior:

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$$1&var2=$2 [L]

That is redirecting / to

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.