Search code examples
apache.htaccessmod-rewritesubdomaintrailing-slash

How do I remove a trailing slash of subdomain URL in htaccess?


Currently, when a request is made for a URL that is not a directory, my .htaccess is set up to remove any trailing slash. The problem is, that is causing test.domain.com/order.php/ to be redirected to test.domain.com/test/order.php instead of test.domain.com/order.php. It works perfectly when not in a subdomain.

Here is the .htaccess code I am using to remove trailing slashes:

    #if it's not a directory
    RewriteCond %{REQUEST_FILENAME} !-d
    #and it has a trailing slash, then redirect to URL without slash
    RewriteRule ^(.+)/$ /$1 [L,R=301]

Somehow, the subdirectory the subdomain serves from is being added to the URL in addition to the subdomain already there. How can I remove the trailing slash without adding /test?

Edit:

Here is the full .htaccess:

    RewriteEngine On

    Options +SymLinksIfOwnerMatch
    Options -Indexes
    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ /$1 [L,R=301]

Solution

  • After seeing anubhava's comment under my question, I decided to put the remove trailing slash rule in an .htaccess file in the subdomain's subdirectory. I also prevent the rule in the document root .htaccess from affecting the subdirectory (since it has its own rule):

        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{HTTP_HOST} !^test\. [NC]
        RewriteRule ^(.+)/$ /$1 [L,R=301]
    

    It now works as intended for the main site, as well as the subdomain.