URL缩短器网站:Htaccess正在将GET请求添加到目录中


URL Shortener website: Htaccess adding GET request to directory

我正在开发一个URL shortener网站,在我的htaccess中,我有以下代码:

RewriteRule ^([a-z0-9]+)$ redirect?code=$1 [NC]

这将从example.com/CODE获取'CODE',并将该代码发送到redirect.php文件,该文件将重定向到包含该代码的url。

我也有一个不同的目录为用户的帐户;/account/以避免服务器混淆(因此它认为example.com/login不应该重定向到url shortener的用户指定代码)。

唯一的问题是,当我访问example.com/account时,它会将?code=account添加到url中,就像这样:example.com/account?code=account,我不完全确定为什么。

有人能帮我吗?我希望将/account url"重定向"到/account/,以便它将其识别为目录。或者,让它与所有目录一起工作,而不仅仅是account。我不知道这是否可能,无论如何谢谢。

您需要避免重写真实的文件和目录,如下所示:

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9]+)/?$ redirect?code=$1 [NC,L,QSA]

您可能想看看这个简单的url shortener,它与您正在做的非常相似。

核心是.htaccess文件:

Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!index'.php).+)$ /index.php?short=$1 [NC,L,QSA]

它从json文件

中动态读取链接