简体   繁体   English

Mod重写规则不起作用

[英]Mod rewrite rule doesn't work

I'm working with mod rewrite, but my code doesn't work. 我正在使用mod重写,但是我的代码不起作用。 It worked for a time. 它工作了一段时间。 I have stripped the code. 我已经剥离了代码。

.htaccess .htaccess

RewriteEngine On 
RewriteBase / 
RewriteRule ^/(.*)/$ index.php?test=$1

PHP 的PHP

<?php
var_dump($_GET['test']);
?>

If I go to the index.php it displays NULL . 如果我转到index.php它将显示NULL

I don't understand why it doesn't work anymore. 我不明白为什么它不再起作用了。 I hope you can help me. 我希望你能帮助我。

Ps I have tested whether the .htaccess file is loaded by making a login form with .htaccess. 附言:我已经通过使用.htaccess登录表单来测试是否加载了.htaccess文件。

You need to get rid of the first slash: 您需要摆脱第一个斜杠:

RewriteRule ^(.*)/$ index.php?test=$1

And even then your rule will only apply when you enter a url that ends with a forward slash, so for example: 而且即使这样,您的规则也仅在输入以正斜杠结尾的网址时适用,例如:

/index.php/

If you want it to work with any url, you need to remove the last slash as well: 如果您希望它与任何网址一起使用,则还需要删除最后一个斜杠:

RewriteRule ^(.*)$ index.php?test=$1

Edit: To avoid rewriting of existing files and directories you need add some conditions: 编辑:为避免重写现有文件和目录,您需要添加一些条件:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?test=$1

You need to remove the first slash in your rule 您需要删除规则中的第一个斜杠

RewriteEngine On 
RewriteBase / 
RewriteRule ^(.*)/$ index.php?test=$1

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

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