If you hate those trailing slashes or perhaps you’re just a fan of good SEO practices then you’ll probably want to remove the trailing slashes from your WordPress installation and edit your NGINX configuration to make sure anything WITH a trailing slash is sent permanently (301 redirect) to the page without a slash.
Just like Google has stated anything with a trailing slash is classed as a directory, and without it is classed as a file. Makes sense, no?
Removing trailing slashes
First, open up the WordPress administration area and navigate to Settings -> Permalinks.
Usually, you’ll have the radio Post name selected which means WordPress will format the URL like so: https://domain.com/sample-post/
. Select the Custom Structure radio and type the corresponding information for example: /%postname%
. Notice we didn’t add a trailing slash? Hit Save Changes.
Now by doing this WordPress has just rewritten all your post, page, and every other link generated automatically by WordPress to remove the trailing slash.
Next we need to tell NGINX that everything WITH a trailing slash needs to be rewritten so that search engines eventually update their indexes with the correct URL structure.
The NGINX code
Simply open up your NGINX configuration file and add the following between server { }
:
if (!-d $request_filename) { rewrite ^/(.*)/$ /$1 permanent; }
The above code first checks to see if the request is a directory (if (!-d $request_filename)
), then rewrites anything with a slash so it no longer has one!
There are other versions of this code flying around the Internet (but if you use the about for WordPress installs you won’t run into any problems) however, without NGINX first checking that it is a real directory, your default /wp-admin/ folder won’t work unless you place index.php after the URL.
Restart NGINX and you’re done!
You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>