使用htaccess的从URL删除的.phphtaccess、URL、php

2023-09-02 11:27:38 作者:神经病人思维广

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

我使用的:

的.htaccess:

.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);

但我得到了$网址是:NULL NULL NULL NULL NULL NULL

But all I get for $url is:NULL NULL NULL NULL NULL NULL

推荐答案

编辑:调整为同时处理重定向和重写

adjusted to handle both the redirect and the rewrite.

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重写时,可能是不可取的。

You should exclude the RewriteCond %{REQUEST_FILENAME} !-d condition, as it will attempt to access a directory if your URL matches it. Probably not desirable when doing url rewriting.

的index.php

index.php

$url = isset($_GET['url']) ? $_GET['url'] : null;
var_dump($url);
 
精彩推荐