Wordpress Snippets

Force Trailing Slash To Requested Urls

Although its personal preference whether you prefer trailing slash at the end of your website URLs or not. But if same webpage content on your website is accessible with two different URLs, one with trailing slash and other without it, then search engines consider it as duplicate content and therefore penalize your website domain in the search engine ranking. Because search engines divide link authority over both links, you will receive less value for the page. So it’s highly recommended that you should keep URL structure same throughout the website for better SEO practice.

When optimizing your website for duplicate content, the trailing slash is very common issue, just like duplicate content caused by domain name with and without www prefix.

We can resolve this issue by allowing visitors to trailing slash URLs only and redirect all incoming traffic without a trailing slash to the same URL.

This can be done by using htaccess. If the URLs don’t have a trailing slash the htaccess will redirect the browser to the URL with a trailing slash. Here is a simple snippet you can add in your .htaccess file to add a trailing slash to your entire website URLs.

# force add trailing slash to requested urls
<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_URI} /+[^\.]+$
    RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
</IfModule>

Hire Us. Or just say Hi!
Need a job? Apply to get one.