You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写htaccess rewrite规则实现论坛profile链接改写并解决404问题

你目前仅配置了旧动态链接到新美观链接的外部301重定向规则,缺少新链接到实际PHP后端文件的内部映射规则,服务器收到/forum/profile/1这类请求时找不到对应实体资源,因此返回404。

你只需要在现有规则基础上补充一条内部重写规则即可,完整规则如下:

Options -MultiViews
RewriteEngine On

# 旧动态链接301跳转到新格式链接
RewriteCond %{THE_REQUEST} \s/forum/profile\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /forum/profile/%1? [R=301,L]

# 新格式链接内部映射到实际PHP文件,无外部跳转
RewriteRule ^forum/profile/([0-9]+)$ /forum/profile.php?id=$1 [L]

如果你的.htaccess文件是直接放在forum目录下而非站点根目录,可以使用简化版规则:

Options -MultiViews
RewriteEngine On

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

RewriteRule ^profile/([0-9]+)$ profile.php?id=$1 [L]

注意测试前先清空浏览器缓存,301为永久重定向,浏览器会缓存旧规则,避免缓存导致测试结果不准。

内容的提问来源于stack exchange,提问作者CyberFox23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 18:24:05