编程语言
首页 > 编程语言> > PHP-Apache Rewrite-本地主机中的干净URL

PHP-Apache Rewrite-本地主机中的干净URL

作者:互联网

在过去的几个小时中,我一直试图在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错误:

Not Found

The requested URL /my_project/test was not found on this server.

Apache/2.2.22 (Ubuntu) Server at localhost Port 80

显然我在这里缺少明显的东西,有帮助吗?

解决方法:

有两件事情要寻找:

>确保已启用.htaccess
>确保上面的代码在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]

标签:apache,mod-rewrite,php,regex,htaccess
来源: https://codeday.me/bug/20191122/2061596.html