Apache 重写 - 在本地主机中清理 URL


Apache rewrite - clean URLs in localhost

在过去的几个小时里,我一直在尝试在我的 LAMP 机器中编写干净的 URL。

Apache 的mod_rewrite已启用,我正在尝试使用 .htaccess 文件将 URL GET 参数传递给我的索引.php脚本当时只是_GET的var_dump。

我目前的.htaccess如下(尽管我已经尝试了很多变体,但我在其他答案上发现了它,但没有成功(

RewriteEngine On
#Make sure it's not an actual file
RewriteCond %{REQUEST_FILENAME} !-f
#Make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d 
#Rewrite the request to index.php
RewriteRule ^(.*)$ index.php?/get=$1 [L]

当我将浏览器指向本地主机/my_project/测试时,我得到的只是一个 404 错误,说:

未找到

在此服务器上找不到请求的 URL/my_project/test。

Apache/2.2.22 (Ubuntu( 服务器位于本地主机端口 80

显然我在这里错过了一些明显的东西,有什么帮助吗?

寻找的两件事:

  1. 确保已启用 .htaccess
  2. 确保上面的代码位于my_project目录中。

然后RewriteBase行也添加:

RewriteEngine On
RewriteBase /my_project/
#Make sure it's not an actual file
RewriteCond %{REQUEST_FILENAME} !-f
#Make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d 
#Rewrite the request to index.php
RewriteRule ^(.*)$ index.php?get=$1 [L]