XHanch Studio Log in | Register | Cart

Forum

Notifications
Clear all

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

XHanch
(@xhanch-alt)
Member Admin

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.

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):

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

Quote
Topic starter Posted : 21/04/2012 1:46 am
XHanch
(@xhanch-alt)
Member Admin

About the / redirection.
I currently have this on my .htaccess

# 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?

# 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:

# 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
ReplyQuote
Topic starter Posted : 21/04/2012 8:34 am
Share:

× Close Menu