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

SEF URLs配置:访问/blog/路径时避免多重选择错误

解决/blog/访问时的多重选择错误方案

现有规则仅匹配blog/后带具体slug的URL,访问https://example.com/blog/(末尾带斜杠)时无对应规则处理,引发服务器多重选择错误。以下是两种实现方案:

方案1:将/blog/指向独立页面blog.php

在现有博客Rewrite规则前添加规则,直接映射到独立页面:

# 处理/blog/访问,指向blog.php
RewriteRule ^blog/$ blog.php [L]

方案2:将/blog/指向blogsearch.php并传递空slug参数

若希望复用blogsearch.php逻辑,传递空slug参数:

# 处理/blog/访问,传递空slug到blogsearch.php
RewriteRule ^blog/$ blogsearch.php?slug= [QSA,L]

修改后的完整.htaccess代码

## set strickt transport policy to force https
<IfModule mod_headers.c>
     Header always set Strict-Transport-Security "max-age=16070400"
</IfModule>


######################## add this to fix variable issue
Options -MultiViews +FollowSymLinks

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

RewriteBase /
RewriteCond %{HTTP_HOST} !^example.com$ [NC]  
RewriteRule .? https://example.com%{REQUEST_URI} [R=301]

# 新增规则:二选一启用
# 方案1:指向独立blog.php页面
RewriteRule ^blog/$ blog.php [L]
# 方案2:指向blogsearch.php传递空slug(注释方案1后启用)
# RewriteRule ^blog/$ blogsearch.php?slug= [QSA,L]

RewriteRule ^blog/([a-zA-Z0-9.\-_]+) blogsearch.php?slug=$1 [QSA]

</IfModule>

关键注意事项

  • 规则顺序必须正确:^blog/$的规则要放在带slug的规则之前,避免被覆盖。
  • [L]标记确保匹配后停止后续规则处理,防止冲突。
  • 若需支持不带斜杠的/blog访问,可额外添加规则:RewriteRule ^blog$ blog/ [R=301,L],统一URL格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 02:12:47