【问题标题】:Rewriting /user to profile.php?u=user with .htaccess使用 .htaccess 将 /user 重写为 profile.php?u=user
【发布时间】:2023-03-13 23:45:01
【问题描述】:

我有一个动态 url 为 (www.abc.com/profile.php?u=me) 的站点,其中 GET 变量“u”指向当前查看的用户(即我)。我希望重写后的 URL 看起来像 www.abc.com/me。 当我使用 URL 生成器时,我主要得到以下内容:

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

它指向 www.abc.com/profile/u/me 并且此页面也不包含 css 文件。 我需要类似 www.abc.com/me 的东西,其中还包括页面的 css 文件。

【问题讨论】:

  • 对不起,我不知道你在问什么。
  • 我有一个带有动态网址的网站,例如 www.abc.com/profile.php?u=user1,但我想使用 url 重写将它们转换为 www.abc.com/user1。这就是我所做的 -- RewriteRule profile/u/(.*) profile.php?u=$1 -- ,但是得到了这个 www.abc.com/profile/u/user1 而不是 www.abc.com/user1

标签: apache .htaccess url mod-rewrite rewriting


【解决方案1】:

试试这个规则:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ profile.php?u=$1 [NC,L]

这会将 example.com/user 重写为 example.com/profile.php?u=user 。

为什么没有加载 css 文件?

这是因为您的相对 URI 的基数已更改。原来,当页面为/profile.php?u=user时,base为/,浏览器用/base正确填充了相关链接。但是当浏览器转到像/user 这样的页面时,基础突然变为/user/,它会尝试将其附加到所有相对 URL 的前面,因此它们都不会加载。

解决方案

您可以将链接设为绝对链接,也可以更改页面标题中的 URI 基础(在 <head> </head> 标记之间):

<base href="/">

【讨论】:

    猜你喜欢
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    相关资源
    最近更新 更多