[How to] to force a trailing slash at the end of your URL with .htaccess mod_rewrite

Started by Xhanch Studio, April 20, 2012, 07:46:47 PM

previous topic - next topic
Go Down

Xhanch Studio

Forcing a trailing slash ("/") at the end of your website's URLs will prevent content/page duplications issue, especially for those who use permalinks.
For example, http://[your-domain]/xxx and http://[your-domain]/xxx/ are the same page with exactly same content.
But, search engine spiders/crawlers will treat those two pages as different pages and search engines hate content duplications.

You can force a trailing slash at the end of your URL with .htaccess mod_rewrite.
Code Select
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

In the case you want to exclude some requests like for PHP, HTML, or other static files files (Recommended):
Code Select
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif|png|css|js)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

Best Regards,
Susanto B.Sc
----------------------------------------------------------------------------
Web development services, WordPress plugin and theme development, PSD to XHTML conversion - http://xhanch.com
Read free manga online - http://authrone.com

Xhanch Studio

Quote from: kaspr on April 20, 2012, 08:09:24 PM
About the / redirection.
I currently have this on my .htaccess
Code Select
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress


Will i just add the rules you put on that link?
So it will look like this?
Code Select
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif|png|css|js)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
</IfModule>
# END WordPress


Or am i wrong?


I would suggest this:
Code Select
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif|png|css|js)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress


Best Regards,
Susanto B.Sc
----------------------------------------------------------------------------
Web development services, WordPress plugin and theme development, PSD to XHTML conversion - http://xhanch.com
Read free manga online - http://authrone.com

Go Up