【问题标题】:How to write htaccess rewrite rule for change link如何为更改链接编写 htaccess 重写规则
【发布时间】:2021-12-23 14:18:17
【问题描述】:

我正在搜索 htaccess 中更改链接的规则。

目前链接如下:

localhost.com/forum/profile.php?id=1

而这就是我想要达到的效果:

localhost.com/forum/profile/1

我添加了以下规则:

Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/forum/profile\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /forum/profile/%1? [R=301,L]

问题是页面自动重定向到预期的链接,但是返回404错误

【问题讨论】:

  • “当前链接看起来像这样” - 澄清一下,如果您还没有更改 HTML 源代码中的实际链接,您仍然需要更改。

标签: .htaccess mod-rewrite url-rewriting


【解决方案1】:

您还需要一条规则来将forum/profile/123 映射到forum/profile\.php?id=123。请在测试您的 URL 之前清除您的浏览器缓存。

Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/forum/profile\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /forum/profile/%1? [R=301,L]
RewriteRule ^forum/profile/([0-9]+)/?$ /forum/profile.php?id=$1 [NC,QSA,L]

【讨论】:

  • 是的,一切正常。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2021-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多