.htaccess在PHP URL中替换(重写)$ _GET变量

het*_*an2 5 php variables .htaccess rewrite

我有一个类似于example.com/index.php?p=testPHP 的URL将使用加载测试变量$_GET['p'].URL已经可以简化为example.com?p=test; 不过,我希望在我简化了这个.htaccesssite.com/test.我该怎么做呢?

Tim*_*per 13

将以下内容放入您的.htaccess文件中:

RewriteEngine on

# The two lines below allow access to existing files on your server, bypassing
# the rewrite

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule (.*) index.php?p=$1 [QSA]
Run Code Online (Sandbox Code Playgroud)

然后,您可以whateverexample.com/whatever以下方式访问:

$value = $_GET['p']; // "whatever"
Run Code Online (Sandbox Code Playgroud)