1

我在我的 Mac 上使用 apache 服务器,一切正常。

现在我有一个网页,它index.php非常复杂: 在index.php中,它需要像这样的 url:localhost/~username/hostname/page_a。其内容page_a实际上是.php存储在content文件夹中的文件。所以在 中index.php,它只是解析page_a,并做一个 include: '' 来呈现请求页面。

但是在.htaccess中,重定向规则没有正确配置。

例如,如果我点击一个链接:localhost/~username/hostname/page_a,有没有办法将这个url重定向到index.php,同时该url仍然是localhost/~username/hostname/page_a,这样index.php才能正确解析和呈现它?

谢谢。

4

1 回答 1

3

这取决于它如何index.php处理请求。如果它只是在$_SERVER变量'REQUEST_URI'中查找请求的文件,那么你不需要传递任何东西。如果它查找查询字符串参数,则您的重写规则需要传递它。

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

或者,如果您需要将查询字符串参数传递给它:

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

如果规则中没有R标志或 an http://,则浏览器上的 URL 地址栏不会更改。如果有人http://localhost/~username/hostname/page_a在他们的浏览器中输入,/~username/hostname/page_a请求将被发送到服务器,重写规则将在内部将其重写为/index.php,并且 php 脚本可以查找请求并正确路由它,或者如果第二个规则在参数中$_SERVER['REQUEST_URI']查找用来。/~username/hostname/page_a$_GET['url']

于 2012-10-03T21:13:17.880 回答