Đinh Carabus
Đinh Carabus

Reputation: 3496

Why is this RewriteRule not working? (xampp)

I have set up this very simple .htaccess-file in my projects root folder c:\xampp\htdocs\myproject\

RewriteEngine on   
RewriteRule ^(.+)$ test.php?stuff=$1 [QSA,L]

as well as a test.php (there is no index.php)

<?php
if (isset($GET_['stuff'])) {
    echo $GET_['stuff'];
} else {
    echo "not set";
}

When I visit localhost/myproject instead of redirecting me to test.php it just lists the directory structure of my project.

When I change the htaccess to something like

RewriteRule ^(.+)$ http://www.google.de [L]

everything works as expected. What am I doing wrong?

Upvotes: 1

Views: 1552

Answers (1)

anubhava
anubhava

Reputation: 785146

Reason why you get directory listing because your URL localhost/myproject actually sends an empty per-dir URI when .htaccess is placed inside /myproject/.

You URI pattern is (.+) that obviously won't match an empty string hence rules doesn't fire and since you don't have index.php or index.html, you get directory list.

Correct rule should be:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ test.php?stuff=$1 [QSA,L]

PS: Note that removing RewriteCond will invoke test.php for localhost/myproject but it will be internally invoked as test.php?stuff=test.php&stuff=

Upvotes: 1

Related Questions