1

我正在使用 Apache 2.2 和 PHP 5.3.8。我应该如何配置 .htaccess 来重定向每个请求做 index.php?

我想要像 stackoverflow 或 facebook 这样的 URL,例如:http://mywebsite.com/fancypage 其中 fancypage 不是服务器上的目录,而只是一个参数。

所以我认为我需要将每个请求重定向到一个 php 页面,其中一些代码可以解释 $_SERVER['REQUEST_URI'] 字符串。这是正确的方法吗?重定向后 $_SERVER['REQUEST_URI'] 是否仍然可用?

4

2 回答 2

5

尝试将以下内容添加到站点根目录中的 .htaccess 文件中。

它将所有请求(现有文件/目录除外)发送到 index.php。

原始请求将在request参数中可用。

RewriteEngine On
RewriteBase /

#skip existing files or directories
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
#everything else goes to index.php
RewriteRule ^ index.php?request=%{THE_REQUEST} [L]
于 2012-01-24T22:21:24.627 回答
1

您感兴趣的是 Apache 的 url_rewrite 函数。我建议您访问这两个链接并进行一些研究:

于 2012-01-24T22:18:31.570 回答