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

为动态.htaccess添加尾部斜杠,解决301循环及多余跳转

URL尾部斜杠导致重复内容及301跳转问题解决历程

背景问题

我维护的网站配置了包含大量动态规则的.htaccess文件,整体运行正常,但Google将带尾部斜杠与不带尾部斜杠的同一URL判定为重复内容。尝试添加尾部斜杠规则时出现301循环,解决循环后又产生多余301跳转,最终通过Cloudflare页面规则并调整.htaccess规则解决问题。

原.htaccess配置

#Options -MultiViews
RewriteEngine On
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ http://www.advogadosaqui.com.br/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

## Adding a trailing slash <<<< (HERE IS WHATS I TRIED) >>>>
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]

# external redirect rule to remove /artigos/ from URLs
RewriteCond %{THE_REQUEST} \s/artigos/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]

# external redirect rule to remove /unidades/pagina_agencia/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_agencia/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]

# external redirect rule to remove /unidades/pagina_locker/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_locker/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]

# external redirect rule to remove /unidades/pagina_estado/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_estado/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]

# external redirect rule to remove /unidades/pagina_cidade/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_cidade/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]

# external redirect rule to remove /unidades/pagina_bairro/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_bairro/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]

# Remove .php extension externally
# To externally redirect /dir/file.php to /dir/file
# %{THE_REQUEST} \s/+(\.+?)\.php[\s?] [NC]
#RewriteRule ^ /%1 [R=301,NE,L]

#Hide and Redirect Extension
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP
RewriteRule ^(.+)\.php$ /$1 [R=301,L]

# internal rewrite from root to /artigos/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/artigos/$1.php -f
RewriteRule ^([\w-]+)/?$ artigos/$1.php [L]

# internal rewrite from root to /unidades/pagina_agencia/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_agencia/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_agencia/$1.php [L]

# internal rewrite from root to /unidades/pagina_locker/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_locker/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_locker/$1.php [L]

# internal rewrite from root to /unidades/pagina_estado/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_estado/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_estado/$1.php [L]

# internal rewrite from root to /unidades/pagina_cidade/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_cidade/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_cidade/$1.php [L]

# internal rewrite from root to /unidades/pagina_bairro/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_bairro/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_bairro/$1.php [L]

# handle .php extension internally
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{DOCUMENT_ROOT}/$1.php -f
#RewriteRule ^(.+?)/?$ $1.php [L]

# remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

##ErrorDocument 404 https://www.advogadosaqui.com.br/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]

首次尝试:添加尾部斜杠规则导致301循环

尝试添加以下规则时,出现301循环问题:

## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]

更新后的问题:多余301跳转

使用Anubhava提供的规则解决循环后,当请求不带尾部斜杠时会产生多余301跳转,无法直接跳转到包含https + www + 尾部斜杠的目标URL。

最终解决方案

通过Cloudflare页面规则接管https及www强制跳转,移除本地.htaccess中对应的规则,并修改尾部斜杠添加规则为:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]

最终.htaccess配置

Options -MultiViews
RewriteEngine On

# Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]

# external redirect rule to remove /artigos/ from URLs
RewriteCond %{THE_REQUEST} \s/artigos/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]

# Remove .php extension externally
RewriteCond %{THE_REQUEST} \s/+(\.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=301,NE,L]

# internal rewrite from root to /artigos/
RewriteCond %{DOCUMENT_ROOT}/artigos/$1.php -f
RewriteRule ^([\w-]+)/?$ artigos/$1.php [L]

# handle .php extension internally
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

调整后成功避免了多余跳转,解决了Google判定重复内容的问题。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 19:20:26