请求帮助:通过.htaccess实现URL的301重定向配置
301重定向规则配置方案
下面针对你的四个重定向需求,分别提供Apache(.htaccess)和Nginx两种常见服务器的配置规则:
Apache(.htaccess)配置
将以下规则添加到网站根目录的.htaccess文件中:
# 1. AMP页面重定向 RewriteRule ^weblog/([^/]+)/amp$ /weblog/$1/ [R=301,L] # 2. 评论页面重定向到锚点 RewriteRule ^weblog/([^/]+)/comment-(\d+)$ /weblog/$1/#comment-$2 [R=301,L,NE] # 3. 分页页面重定向到博客根目录 RewriteRule ^weblog/page/\d+$ /weblog/ [R=301,L] # 4. 带#!/ccomment的旧URL重定向 RewriteCond %{REQUEST_URI} ^/weblog/([^/]+)/$ RewriteCond %{QUERY_STRING} ^#!/ccomment-comment=\d+$ RewriteRule ^ /weblog/%1/ [R=301,L]
说明:
NE标记用于确保锚点中的#不会被转义,保证浏览器能正确识别锚点位置。
Nginx配置
将以下规则添加到你的站点server块中:
# 1. AMP页面重定向 rewrite ^/weblog/([^/]+)/amp$ /weblog/$1/ permanent; # 2. 评论页面重定向到锚点 rewrite ^/weblog/([^/]+)/comment-(\d+)$ /weblog/$1/#comment-$2 permanent; # 3. 分页页面重定向到博客根目录 rewrite ^/weblog/page/\d+$ /weblog/ permanent; # 4. 带#!/ccomment的旧URL重定向 if ($request_uri ~* ^/weblog/([^/]+)/#!/ccomment-comment=\d+$) { rewrite ^ /weblog/$1/ permanent; }
说明:Nginx中
permanent对应301永久重定向,锚点会被自动保留无需额外转义。
内容的提问来源于stack exchange,提问作者Sarah
相关产品推荐
相关产品推荐

