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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

To 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