将文件名视为PHP GET

将文件名视为PHP GET,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我目前在测试网站设置中有以下格式的配置文件: example.com/profile.php?u=username example.com/profile/username 我想使用以下格式: example.com/profile.php?u=username example.com/profile/username 如何将用户名视为php中的变量,而不是文件?您可以使用以下内容: RewriteCond %{REQUEST_URI} ^/profile/(.*)$ RewriteRu

我目前在测试网站设置中有以下格式的配置文件:

example.com/profile.php?u=username
example.com/profile/username
我想使用以下格式:

example.com/profile.php?u=username
example.com/profile/username

如何将
用户名
视为php中的变量,而不是文件?

您可以使用以下内容:

RewriteCond %{REQUEST_URI} ^/profile/(.*)$
RewriteRule ^(.*)$ /profile.php?u=%1 [L]

RewriteEngine on
RewriteRule ^/profile/(.*)$ /profile.php?u=$1 [QSA,L]
您需要使用apache

在网站的根目录上创建名为
.htaccess
的文件,并添加以下内容:

RewriteEngine on
RewriteRule ^/profile/(.*)$ /profile.php?u=$1 [QSA,L]
现在,如果导航到:

example.com/profile/usertest

apache将把变量
usertest
发送到
profile.php?u=usertest

对我不起作用。我已安装mod_rewrite,将allowoverride设置为all,并在.htaccess和apache2.conf中进行了尝试。请定义“不适用于我”,同时,将日志文件添加到
.htaccess
,即:
RewriteLog”/path/to/log/somepath.log“RewriteLogLevel 9
。请确保日志可写,并在重试后检查日志,它显示了什么?
RewriteLog
RewriteLogLevel
已被删除。我将
LogLevel
更改为
debug
,并进入error.log。我所说的“对我不起作用”的意思是,如果我没有这个规则,结果也没有什么不同;服务器抛出一个404。删除
RewriteRule
中的前导斜杠。只有当您直接在
httpd.conf
中编写时,或者在我认为Apache 2.4之前,这才是必需的。这里你的规则永远不会匹配,因为没有前导斜杠