admin管理员组文章数量:1122846
I'm trying to avoid duplicate content on my blog caused by trailing slashes. Currently, these URLs display the same content:
- /
- //
I found some custom code for the .htaccess file, but it's not working as expected, especially in incognito mode:
RewriteEngine on
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+?)/$ /$1 [R=301,NE,L]
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule ^ %1/%2 [R=301,L]
I want to ensure only one trailing slash. Any help is appreciated!
I'm trying to avoid duplicate content on my blog caused by trailing slashes. Currently, these URLs display the same content:
- https://espartedelviaje.com/que-hacer-en-sydney
- https://espartedelviaje.com/que-hacer-en-sydney/
- https://espartedelviaje.com/que-hacer-en-sydney//
I found some custom code for the .htaccess file, but it's not working as expected, especially in incognito mode:
RewriteEngine on
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+?)/$ /$1 [R=301,NE,L]
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule ^ %1/%2 [R=301,L]
I want to ensure only one trailing slash. Any help is appreciated!
Share Improve this question asked Jun 11, 2024 at 8:00 DiegoBDiegoB 132 bronze badges1 Answer
Reset to default 1To ensure that your URLs have a consistent trailing slash and avoid duplicate content, you can use the following .htaccess
rules. These rules will enforce a single trailing slash for URLs and redirect any URLs without or with multiple trailing slashes to their correct form.
Here's the updated .htaccess
code:
# Enable rewrite engine
RewriteEngine On
# Remove multiple trailing slashes
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule ^ %1/%2 [R=301,L]
# Redirect URLs with trailing slash to without (only for URLs not ending with a file extension)
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+?)/$ /$1 [R=301,NE,L]
# Add trailing slash if not present (except for existing files and URLs with file extensions)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1/ [R=301,L]
本文标签: apacheAvoid duplicate content on pretty URLs with htaccess
版权声明:本文标题:apache - Avoid duplicate content on pretty URLs with htaccess 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303995a1932072.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论