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]
Related Posts:
- Rewrite /?rest_route=/ link to /wp-json/ without changing default permalink structure in apache
- index.php not loading in main folder of wordpress
- Redirect from different port to subdomain – htaccess
- WordPress 404 on Subdomain
- Only expose routes with prefix /wp-json on WordPress using Apache
- What’s the opposite of required valid user in .htaccess authentication
- How can i redirect one url to another url using .htaccess or add_rewrite_rule
- Override htacces rule only for specific directory
- Restrict uploaded files into a custom folder to logged in users by htaccess: looking for Nginx – not only Apache – solution
- How do I test to ensure that my wp-config file is protected?
- How do I host WordPress on a hidden domain through a reverse proxy?
- .htaccess Security Header Rules
- mod_rewrite loop, redirecting http to https on certain section of wordpress blog
- .htaccess in subdir gets ignored by WordPress’ own .htaccess in /
- Directing subdomain to main domain and keeping the subdomain format with .htaccess
- WordPress – Promoting A Dev Build In A Subdirectory To Production / Root Directory
- Redirect old domain with query paramaters
- Does WP suppresses .htaccess if permalinks are disabled?
- fix 302 redirection error on https
- Access sub-domain when root public_html is protected with .htaccess password
- Accepting special characters in querystring
- WordPress permalinks confusion
- Configure .htaccess to have a WordPress single site installation with all subdomains pointing to the same pages?
- Unable leverage Browser Caching on AWS Bitnami stack (Apache) through W3TC and Cloudfront CDN
- How to move wordpress website from hosting account to localhost
- How to block wordpress admin by htaccess
- I can access subdirectory, but not files within it
- WordPress redirection
- How To Add CSP frame ancestors in WordPress Website? [closed]
- Subfolder install not working
- Browser Caching .htaccess
- Hardening WordPress – how to set .htaccess permissions?
- .htaccess redirect http to https
- Redirect old php link to wordpress link in .htaccess
- Place static HTML files in path below WordPress page
- Static raw HTML page
- htaccess rewrite for author query string when WP is in subfolder
- Why “Settings->Permalinks” creates .htaccess file on nginx server?
- .htaccess for wordpress inside another wordpress install
- .htaccess file redirecting to parent directory
- WordPress in sub directory wp-admin problem
- Rewrite rule not working
- Using “wordpress_logged_in” to restrict direct access to uploads folder in 2021
- Temporary .htaccess blocking is disabling WP Crons from running?
- .htaccess redirects disappeared after re-saving permalinks
- How have I misconfigured basic auth for my wordpress site?
- WordPress multilingual website domain and folders
- Attach to wp-login.php and xmlrpc.php
- XMLRPC filtering through htaccess not working
- Corrupt .htaccess file
- Cache policy not updated according to PageSpeed
- How to redirect http://mydomain/blog/blahblah/ to http://mydomain/blahblah/ in wordpress htaccess?
- When is it necessary to have Header unset Vary in .htaccess
- Redirect http to https does not work on subdir where another instance of WordPress installed
- redirect the homepage using .htaccess outside of WordPress
- Https Redirect infinite loop in Mobile browsers
- 403 error on admin login page
- Apache Fallback instead of add_rewrite_rule
- Redirect an old link to new site homepage [closed]
- .htaccess rewriting old RSS feed URL to WordPress feed URL
- How can I set Cache TTL for woff and woff2 font files with htaccess?
- htaccess working on local server but not on live server
- How can I fix the redirect chain after implementing ssl on wordpress?
- How do I configure wordpress structure for development using git and composer
- How to create a redirect in the .htaccess file, with 2 exceptions
- My Homepage Suddenly Disappeared and I Can’t Get It Back
- How to turn this .htaccess rule into a dynamic rule with add_rewrite_rule, et al?
- Disable directory browsing of uploads folder
- Local PC cache stays filled with old WordPress Site data
- How to have a custom URI path for specific page template
- WordPress JSON API restrict to specific domain
- Where to put W3 Total Cache rewrite rules in .htaccess? [closed]
- Non WordPress Folder in a WordPress Site
- Rewrite Rules not redirecting rewrite
- How do i allow access to a single file in my root directory? [closed]
- Change root directory
- Selectively Disabling PHP via .htaccess in Root Directory
- What’s the best way to manage a lot of 301 redirects in WordPress?
- Shared hosting, multiple sites, can’t log in to WP due to .htaccess redirection
- What causes 404 errors that forces you to rebuild a .htaccess file?
- How to set up MS Exchange Autodiscover alongside WordPress
- Xampp is not loading media
- PHP application in sub directory keeps redirecting to main site
- Blocking wp-login in HTACCESS has also blocked password protected pages
- execute cron jobs when .htaccess login protected?
- Redirect WordPress site to a landing (construction) page using htaccess, with access to /wp-admin and /invoice
- change URIs of migrated site
- htaccess- to hide subdirectory slug only from the post
- Subfolder renaming
- Steps for WordPress over SSL
- Fixing Access-Control-Allow-Origin (CORS origin) for multiple subdomains
- Redirect Loop in Regex Moving to HTTPS
- insert_with_markers() WordPress & htaccess help
- WordPress login bug. Need an emergency solution
- Cannot find webarh/malware redirect infection that only shows on Chrome
- Debug errors for “Destination directory for file streaming does not exist or is not writable”
- WordPress Media Library rendering blank image tiles
- Redirect loops in Bing holding my sites back
- How do I modify each instance of setcookie?
- Permalinks not working on debian with OVH