Php 为什么赢了';这条简单的规则行得通吗?

Php 为什么赢了';这条简单的规则行得通吗?,php,.htaccess,mod-rewrite,Php,.htaccess,Mod Rewrite,这应该很简单,但我想不出来。我有一个非常简单的mod_重写规则,它就是不想工作。这是.htaccess文件的全部内容 RewriteEngine On RewriteRule ^([A-Za-z0-9-_/\.]+)/?$ index.php?page=$1 [L] 如果我调用URL domain.com/foo,它应该将其重写为index.php?page=foo。但是,它将其重写为index.php?page=index.php。我尝试了多个URL: php?page=foo index

这应该很简单,但我想不出来。我有一个非常简单的mod_重写规则,它就是不想工作。这是.htaccess文件的全部内容

RewriteEngine On
RewriteRule ^([A-Za-z0-9-_/\.]+)/?$ index.php?page=$1 [L]
如果我调用URL domain.com/foo,它应该将其重写为index.php?page=foo。但是,它将其重写为index.php?page=index.php。我尝试了多个URL:

  • php?page=foo
  • index.php
  • /福
  • /
在所有情况下,PHP的作用就像“page”被设置为“index.PHP”。index.php不是一个错误,因为我用一个脚本替换了index.php的全部内容,这个脚本简单地响应了“page”的值,结果仍然是index.php

我真的迷失了方向,任何帮助都会很棒

谢谢


阿德里安我想这就是你要找的。不幸的是,如果URL有其他GET参数(例如,/some/URL/path?page=1),它就不能很好地工作

更好的主意。

RewriteEngine On
RewriteRule ^(.*)$ index.php [L,QSA]
QSA标志将转发GET参数,然后在index.php中使用
$\u SERVER['REQUEST\u URI']
parse\u url
来路由请求

发件人:


您不能使用更简单的工具的任何原因,例如:

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

如果您试图阻止重新写入现有页面,则
-f
将处理该问题(
如果文件不存在
并且
如果目录不存在
,那么
重新写入

暂时我不会有多个get参数,我现在只需要它。我尝试了你给我的东西,结果仍然是一样的。实际上我没有想到这一点(避免重新编写实际页面),但这绝对是我需要做的事情!这对我有用,谢谢!
'qsappend|QSA' (query string append)
This flag forces the rewriting engine to append a query string part
in the substitution string to the existing one instead of replacing it.
Use this when you want to add more data to the query string via a rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]