【问题标题】:Implementing URL rewriting with wamp and php用 wamp 和 php 实现 URL 重写
【发布时间】:2015-10-31 07:40:13
【问题描述】:
我无法实现 URL 重写。
到目前为止我做了什么:
1. 在 apache 中启用 rewrite_module。
2.在我的网站(test)目录下添加了.htaccess文件:
127.0.0.1/test/
其中包含以下内容:
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
3. 我有一个 news.php 文件,其中包含
echo "hello world";
现在,每当我访问 URL:http://127.0.0.1/news/1,我都会得到一个
'找不到页面'
【问题讨论】:
标签:
php
apache
mod-rewrite
url-rewriting
wampserver
【解决方案1】:
我成功了。
文件:
document_root/
|- .htaccess
|- news.php
.htaccess:
RewriteEngine on
RewriteRule news/([0-9]+)\.html news\.php?news_id=$1
http://127.0.0.1/news/1.html
->你好世界
【解决方案2】:
我想你已经成功地启用了你的 Apache 服务器的重写模块 (rewrite_module),并且你已经把你的 .htaccess 文件放在了你的 web 根目录下的 test 目录中服务器(例如:c:\wamp\www\test)。
所以这里我们有 2 个案例(我想)你想要做什么:
- 使用例如:
http://127.0.0.1/test/news/1.html 访问您的 URL。
- 使用例如:
http://127.0.0.1/news/1.html 访问您的 URL。
因此,对于第一种情况,您只需像这样编辑您的 .htaccess 文件:
RewriteEngine on
RewriteRule ^news/([0-9]+)\.html news.php?news_id=$1
在这里,任何像 http://127.0.0.1/test/news/1.html 这样的 URL 都将重写为 test\news.php 并将 news_id 参数传递给它。
对于第二种情况,您必须将 .htaccess 文件放入 Web 服务器根目录并仅添加 RewriteBase 指令
RewriteBase 指令指定用于替代相对路径的每个目录 (htaccess) 的 RewriteRule 指令的 URL 前缀。
所以你的.htaccess 会是这样的:
RewriteEngine on
RewriteBase /test
RewriteRule ^news/([0-9]+)\.html news.php?news_id=$1
当然,你可以在网上找到很多关于如何使用.htaccess 文件重写玩具 URL 的教程...
希望能有所帮助。