如何使用GET参数将HTTP请求重定向到静态文件


How to redirect a HTTP Request to a static File with the GET Parameter

我有一个页面

example.com/u/_USERNAME_

我想将请求重定向到

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

我尝试了一个.htaccess(在/u/文件夹中)文件,其中包含以下内容:

RewriteEngine on
RewriteRule (.*)$ profile.php?u=$1

它重定向请求但是$_GET['u']变量包含profile.php

问题出在哪里?


我已经解决了问题:

文件profile.php位于/u/文件夹中。因此,规则重定向到自己的文件,GET参数就会丢失。我已经把profile.php放在根文件夹example.com/中,它就工作了!

apache重写规则是正则表达式,您编写的规则与整个字符串匹配。写一条规则,匹配/u/因此so之后的内容:

RewriteRule ^u/([^/]*)$ /u/profile.php?u=$1 [L]