php - Apache 重写 - 本地主机中的干净 URL

标签 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 错误:

Not Found

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

Apache/2.2.22 (Ubuntu) Server at localhost Port 80

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

最佳答案

要寻找的 2 件事:

  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]

关于php - Apache 重写 - 本地主机中的干净 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19211824/

相关文章:

php - 关于如何使用mysql和php从数据库显示成员信息的问题?

php - 使用 Shopify API 在 PHP 中创建产品

java - 重复替换调用导致 java.lang.OutOfMemoryError

apache - Maven 3.5.2 无法将 Artifact 部署到 Artifactory,出现错误 417

java - 如何防止PasswordTextField在提交表单时清除文本?

php - Composer - 如何将自己的包添加到列表中

php - 防止使用 PHP CURL 的远程脚本登录网站

c# - 提取脚本标签的正则表达式

regex - 带有约束的 Rails 自定义路由 - 路由要求中不允许使用正则表达式 anchor 字符

java - Apache Velocity - 根据 Foreach 总数解析 VM 文件