Search code examples
.htaccessmod-rewritehref

When force trailing slash my href is changed


My .htaccess code is the following:

  Options +FollowSymLinks 
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^([^\.]+)/$ $1.php

It works good for me since if i use the www.domain.com/login link, I get back the following www.domain.com/login/ which is good. The only problem is, that my href link becomes: www.domain.com/login/login which is very bad. Of course when I click on it I get the 404 error which is normal.

Can anyone help to avoid this problem!


Solution

  • Sounds like a relative vs absolute path issue. When you get redirected to /login/, the base URI becomes /login/ instead of just /. So either make your href an absolute URI or specify a base:

    <a href="/login/">link</a>
    

    or add this to the header of your page:

    <base href="/">