Last updated on September 13, 2023

Remove Trailing Slash from URL

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Remove trailing slash from your URL.

To remove the trailing slash from your WordPress URL, you can modify the .htaccess file in the root directory of your WordPress installation. Here’s an example code snippet that you can add to the .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

In this code snippet, we use the mod_rewrite module to remove the trailing slash from the WordPress URL. We first add the RewriteBase directive to set the base directory for the rewrite rules, and then we add a RewriteCond directive to exclude requests for directories that actually exist on the server.

Next, we add a RewriteRule to match URLs that end with a slash and redirect them to the same URL without the trailing slash, using a 301 (permanent) redirect. This ensures that any existing links to URLs with a trailing slash are redirected to the new URL format.

Finally, we add another RewriteCond directive to exclude requests for files that actually exist on the server, and a final RewriteRule to rewrite all other requests to index.php.

By adding this code snippet to your .htaccess file, you can remove the trailing slash from your site’s URL and create cleaner, more user-friendly URLs for your site.

Last updated on September 13, 2023. Originally posted on May 12, 2023.

Leave a Reply