php - 使用 htaccess 从 URL 中删除 .php

标签 php apache .htaccess url-rewriting xampp

我的网址:http://localhost/test.php

我正在使用:

.htaccess:

RewriteEngine On 
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

PHP:

$url = $_GET['url'];
echo var_dump($url);

但是我得到的 $url 是:NULL NULL NULL NULL NULL NULL

最佳答案

编辑:调整以处理重定向和重写。

RewriteEngine On 
RewriteBase /

# Redirect .php URLs to rewritten URLs
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+)\.php$ $1 [L,QSA,R=301]

# Rewrite URLs for processing by router (index.php)
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L,NC]

您应该排除 RewriteCond %{REQUEST_FILENAME} !-d 条件,因为如果您的 URL 匹配它,它将尝试访问目录。进行 url 重写时可能不可取。

索引.php

$url = isset($_GET['url']) ? $_GET['url'] : null;
var_dump($url);

关于php - 使用 htaccess 从 URL 中删除 .php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16555414/

相关文章:

php - mysql 将同一个表与另一个表连接

php - MySQL 循环使用分组依据

php - Laravel 5.3 - ClassLoader.php 第 414 行中的 ErrorException

apache - httpd - 文件存在时不触发 if 子句

未找到 Android 库

apache - mod_rewrite url 中带有空格

php - .htaccess错误无法按预期工作

linux - 虚拟分钟 : multiple subdomains to single directory

apache - 如何重写htaccess规则以重定向到css文件

php - Laravel 流明照亮/控制台不会更新