简体   繁体   English

为什么这个简单的RewriteRule不起作用?

[英]Why won't this simple RewriteRule work?

This should be simple but I cannot figure it out. 这应该很简单,但我无法弄清楚。 I have this very simple mod_rewrite rule and it just doesn't want to work. 我有这个非常简单的mod_rewrite规则,它只是不想工作。 This is the entire contents of the .htaccess file. 这是.htaccess文件的全部内容。

RewriteEngine On
RewriteRule ^([A-Za-z0-9-_/\.]+)/?$ index.php?page=$1 [L]

If I call the URL domain.com/foo it should rewrite it to index.php?page=foo. 如果我调用URL domain.com/foo,则应将其重写为index.php?page = foo。 But instead, it rewrites it to index.php?page=index.php. 而是将其重写为index.php?page = index.php。 I have tried multiple URLs: 我尝试了多个URL:

  • index.php?page=foo 的index.php?页= FOO
  • index.php 的index.php
  • /foo /富
  • / /

In all cases, PHP acts as if 'page' is set to "index.php". 在所有情况下,PHP都将'page'设置为“ index.php”。 It isn't a fault with index.php because I replaced the entire contents of index.php with a script that simple echoed the value of 'page' and it still comes out as index.php. 这不是index.php的问题,因为我用简单的脚本替换了index.php的全部内容,但该脚本仍简单地回显了'page'的值,但仍以index.php的形式出现。

Really lost where I'm going wrong here, any help would be awesome! 我真的错在这里错了,任何帮助都太好了!

Thanks 谢谢

Adrian 阿德里安

Any reason you can't use something simpler, such as: 不能使用任何简单原因的任何原因,例如:

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

If you're trying to keep existing pages from being re-written, then the !-f will take care of that ( if file does not exist and if directory does not exist , then re-write ) 如果您试图防止现有页面被重写,则!-f将解决这一问题( if file does not existif directory does not exist ,则re-write

I think this is what your looking for. 我认为这是您想要的。 Unfortunately it doesn't really work well if the URL has other GET arguments (IE, /some/url/path?page=1). 不幸的是,如果URL具有其他GET参数(IE,/ some / url / path?page = 1),则不能很好地工作。

RewriteEngine On
RewriteRule ^(.*)$ index.php?page=$1 [L]

Better idea. 更好的主意。

RewriteEngine On
RewriteRule ^(.*)$ index.php [L,QSA]

The QSA flag will forward the GET params and then in index.php use $_SERVER['REQUEST_URI'] and parse_url to route the request. QSA标志将转发GET参数,然后在index.php中使用$_SERVER['REQUEST_URI']parse_url路由请求。

From http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html : http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

'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.

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

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