【问题标题】:Redirect Users From Old URL To New将用户从旧 URL 重定向到新 URL
【发布时间】:2014-04-10 00:23:07
【问题描述】:

每个用户都可以通过 www.example.com/profile/{username} 访问他们的帐户资料

注意:用户名仅包含数字和字母

例如: www.example.com/profile/michaelwatson www.example.com/profile/jenniferlee2

想从旧网址重定向: www.example.com/profile/jenniferlee2

到新的更短的 URL www.example.com/jenniferlee2

我希望任何拥有旧书签的人从旧 URL 转到新 URL,并且任何人都在寻找但只查找个人资料 URL。其他任何内容都应保持不变。

这是我尝试添加到 .htaccess 但出现内部服务器错误

重写规则 (.*) /profile/$1 [L]

【问题讨论】:

  • 你怎么知道配置文件不是带有旧 URL 的新 URL 的配置文件?例如,如果我访问 http://www.example.com/contact 并且由 index.php 处理,我将根据您当前的规则被重定向到 /profile/contact

标签: .htaccess url redirect rewrite


【解决方案1】:

您可以在 root .htaccess 中使用此代码:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+profile/(\w+) [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/profile/
RewriteRule ^(\w+)$ /profile/$1 [L]

【讨论】:

  • 尝试了您的建议,但它破坏了其他链接(将 /profile 添加到其他链接)。如果我点击example.com/account/lostpass,它会显示在此服务器上找不到请求的 URL /profile/account/lostpass。
  • 但是你没有提供任何机制什么时候放/profile,什么时候不放?你能澄清一下吗?
  • 您能否解释一下您所指的机制,以便我提供正确的信息?
  • 我想知道哪些 URI 模式应该重写为/profile
  • 我已经编辑了我的第一篇文章以使其更清晰。这是为每个人显示的格式:example.com/profile/username
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
相关资源
最近更新 更多