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

2 Posts
1 Users
0 Likes
10.2 K Views
XHanch
(@xhanch-alt)
Posts: 2105
Member Admin
Topic starter
 

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]

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

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

× Close Menu