简体   繁体   English

为什么这个RewriteRule不起作用? (XAMPP)

[英]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\\ 我已经在我的项目根文件夹c:\\xampp\\htdocs\\myproject\\设置了这个非常简单的.htaccess文件

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

as well as a test.php (there is no index.php) 以及一个test.php (没有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. 当我访问localhost/myproject而不是将我重定向到test.php它仅列出了我的项目的目录结构。

When I change the htaccess to something like 当我将htaccess更改为类似

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/ . 之所以会得到目录列表,是因为将.htaccess放在/myproject/时,URL localhost/myproject实际上会发送一个空的按目录URI。

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. 您的URI模式是(.+) ,显然不会与空字符串匹配,因此规则不会触发,并且由于您没有index.phpindex.html ,因此您会获得目录列表。

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= PS:请注意,删除RewriteCond将为localhost/myproject调用test.php ,但它将在内部作为test.php?stuff=test.php&stuff=调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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