简体   繁体   中英

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?

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=

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM