我的网址: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/