2010-08-11 72 views
0

这应该很简单,但我无法弄清楚。我有这个非常简单的mod_rewrite规则,它只是不想工作。这是.htaccess文件的全部内容。为什么这个简单的RewriteRule不能工作?

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。我曾尝试多个网址:

  • index.php页面= foo的
  • 的index.php
  • /富
  • /

在所有情况下,PHP行为,如果页面'设置为“index.php”。这不是index.php的错,因为我用一个脚本来替换index.php的全部内容,这个脚本简单地回应了'page'的值,它仍然以index.php的形式出现。

真的失去了我要去错的地方,任何帮助都会很棒!

感谢

阿德里安

回答

2

你不能用简单的东西,如任何原因:

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

如果你想保持被改写现有的网页,然后!-f将照顾(if file does not existif directory does not exist,然后re-write

+0

我其实并没有想到(避免重写实际页面),但这绝对是我需要做的事情!这对我有用,谢谢! – 2010-08-11 03:39:01

0

我觉得这是你在找什么。不幸的是,如果URL有其他的GET参数(IE,/ some/url/path?page = 1),它并不能很好地工作。

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

更好的主意。

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

的QSA标志将转发GET PARAMS,然后在index.php中使用$_SERVER['REQUEST_URI']parse_url将请求路由。

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. 
+0

暂时我不会有多得到论据,我只需要这个为现在工作。我试过你给我的东西,它仍然产生相同的结果。 – 2010-08-11 03:35:43

相关问题