Search code examples
apache.htaccessdrupal

Don't redirect to www if it has a certain path via htaccess


The following rule in my .htacess file, for my Drupal websites, redirects all non www URLs to the www version. (Ex: https://example.com -> https://www.example.com):

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This works great but now I want to exclude https://example.com/samltest/test from redirecting to www. I added the following:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !/samltest/ [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

But now when I got to https://example.com/samltest/test, it redirects to https://example.com/index.php. Does anyone have any ideas on what I'm doing wrong?


Solution

  • Have it like this at the top of your .htaccess:

    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{THE_REQUEST} !\s/+samltest [NC]
    RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    

    Test it after clearing your browser cache.