Apache重写 - 在localhost中清除URL

时间:2013-10-06 17:40:28

标签: php regex apache .htaccess mod-rewrite

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

Apache的mod_rewrite已启用,我正在尝试使用.htaccess文件将URL GET参数传递给我的index.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]

当我将浏览器指向localhost / my_project / test时,我得到的是404错误说:

  

未找到

     

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

     

localhost端口80上的Apache / 2.2.22(Ubuntu)服务器

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

1 个答案:

答案 0 :(得分:3)

要寻找的两件事:

  1. 确保已启用.htaccess
  2. 确保上面的代码位于my_project目录。
  3. 然后添加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]