htaccess disallow redirect specific subdomain

Viewed 45

I redirect all virtual subdomains as follows.

RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com$
RewriteRule ^$  subdomain.php?url=%1  [NC,QSA,L]

This way, I get data from the database. All "virtual subdomains". But there is a real subdomain. Opencart inside (shop.mysite.com)

htaccess directs this as well. How do I prevent it from just redirecting the word "shop".

My Full Code:

Options Indexes SymLinksIfOwnerMatch
RewriteEngine on
directoryindex index.php
Options -indexes
RewriteRule index index.php
RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com$
RewriteRule ^$  subdomain.php?url=%1  [NC,QSA,L]

# www.site.com  >>  site.com
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
1 Answers
RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com$
RewriteRule ^$  subdomain.php?url=%1  [NC,QSA,L]

You can add another condition to exclude the shop subdomain. For example:

RewriteCond %{HTTP_HOST} !^shop\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^$ subdomain.php?url=%1  [QSA,L]

The ! prefix negates the regex, so the condition is successful when it does not match.

OR, you could use a negative lookahead in your original condition regex. For example:

RewriteCond %{HTTP_HOST} ^((?!shop\.).+)\.example\.com$
RewriteRule ^$ subdomain.php?url=%1  [QSA,L]

However, your directives are also in the wrong order. Your external redirect to remove the www subdomain should appear first, otherwise you need to incorporate another exception into the above rule.

For example:

Options SymLinksIfOwnerMatch
DirectoryIndex index.php

RewriteEngine on

RewriteRule index index.php

# www.site.com  >>  site.com
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^shop\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^$ subdomain.php?url=%1  [QSA,L]

You were also adding the Indexes option, only to remove it again with a later directive.

Related