Search code examples
.htaccesstrailing-slashduplicate-content

Removing trailing slash by htaccess


Depending on google suggestions, I tried to add some codes to my .htaccess to prevent duplicate content. This is my code:

#Duplicate Content
RewriteCond %{HTTP_HOST} !^www.soomar63.com$ [NC]
RewriteRule ^(.*)$ http://www.soomar63.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ 
RewriteRule ^index\.php$ http://www.soomar63.com/ [R=301,L]
#Remove Trailing Slash
RewriteCond %{HTTP_HOST} ^(www.)?soomar63\.com$ [NC]
RewriteCond %{THE_REQUEST} !administrator/(.*)$
RewriteRule ^(.*)/$ http://www.soomar63.com/$1 [R=301,L]

This code is working fine, it have just a little problem:

This code corrects following links:

soomar63.com -> www.soomar63.com
soomar63.com/ -> www.soomar63.com
soomar63.com/index.php/ -> www.soomar63.com
soomar63.com/about.html/ -> www.soomar63.com/about.html

But it dosen't corrects following links:

soomar63.com/index.php?mylink/ -> must correct to www.soomar63.com/index.php?mylink
soomar63.com/about.html#mylink/ -> must correct to www.soomar63.com/about.html#mylink

Please help. Thank you.


Solution

  • Replace your #Remove Trailing Slash rules with below

    #Remove Trailing Slash
    
    RewriteCond %{HTTP_HOST} ^(www.)?soomar63\.com$ [NC]
    RewriteCond %{THE_REQUEST} !^/?administrator$
    RewriteRule ^ http://www.soomar63.com%{REQUEST_URI} [R=301,L,QSA]