Search code examples
.htaccess

Removing trailing slash from htaccess redirection


I want to make a redirection using htaccess but I run into an issue with trailing slash.

I want to redirect from https://test.com/transfer or https://test.com/transfer/ to https://test2.com/destination Below is my htaccess rule but it seems to be only working for https://test.com/transfer but NOT https://test.com/transfer/

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI}  ^/transfer$ [NC]
RewriteRule ^ https://somesite.com/destination/$1 [R,L]

What is wrong with my rule?


Solution

  • Try:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI}  ^/transfer/?$ [NC]
    RewriteRule ^ https://somesite.com/destination/$1 [R,L]
    

    or

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/transfer/?$ https://somesite.com/destination/ [NC,R,L]
    

    Make sure you use RewriteEngine On only once.

    If you want to redirect everything inside transfer: if transfer is a dir btw, use:

    RewriteEngine on
    RewriteRule ^/transfer/?(.+)?$ https://somesite.com/destination/$1 [R, L]