charlie
charlie

Reputation: 481

htaccess will not rewrite subdomains

I currently have this code in my htaccess file stored in the root:

RewriteEngine On
DirectoryIndex index.php

RewriteCond %{HTTP_HOST} admin.mydom.com$
RewriteRule ^(.*)$ /admin/system/$1 [L]

RewriteCond %{HTTP_HOST} ^([^/.]+)\.mydom\.com$
RewriteRule ^(.+)$ /subdomains/%1/ [L]

RewriteCond %{HTTP_HOST} !mydom.com$ [NC]
RewriteRule ^(.*)$ http://www.mydom.com/$1 [L,R=301]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

The file should ensure the following is done on the site:

admin. subdomain is mapped to /admin/system

map all subdomains to /subdomains/{subdomain}

redirect any domain other than mydom.com to mydom.com

and then the standard wordpress htaccess

Its working fine apart from the subdomain part, all subdomains are being redirected back to the main domain (mydom.com)

Upvotes: 2

Views: 117

Answers (3)

anubhava
anubhava

Reputation: 786291

Have it like this:

DirectoryIndex index.php

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ [NC]
RewriteRule ^ http://www.mydom.com%{REQUEST_URI} [L,R=301,NE]

# redirect sub.dom2.com to sub.mydom.com
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(?!mydom\.)[^.]+\. [NC]
RewriteRule ^ http://%1.mydom.com%{REQUEST_URI} [L,NE,R=302]

RewriteCond %{HTTP_HOST} admin\.mydom\.com$ [NC]
RewriteRule !^admin/system/ admin/system%{REQUEST_URI} [L,NC]

RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.mydom\.com$ [NC]
RewriteRule !^subdomains/ subdomains/%1%{REQUEST_URI} [L,NC]

RewriteRule ^(subdomains/|admin/|index\.php$) - [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Upvotes: 1

Amit Verma
Amit Verma

Reputation: 41249

You can use the following rules :

RewriteEngine On
DirectoryIndex index.php

RewriteCond %{HTTP_HOST} ^admin\.mydom.com$
RewriteRule ^((?!admin/system).*)$ /admin/system/$1 [L]

RewriteCond %{HTTP_HOST} ^((?!www\.).+)\.mydom\.com$
RewriteRule ^(.+)$ /subdomains/%1/ [L]

RewriteCond %{HTTP_HOST} !mydom.com$ [NC]
RewriteRule ^(.*)$ http://www.mydom.com/$1 [L,R=301]

Clear your browser's cache before testing this.

Upvotes: 0

Croises
Croises

Reputation: 18671

You can try to change the beginning:

RewriteEngine On
DirectoryIndex index.php

RewriteCond %{HTTP_HOST} admin\.mydom\.com$ [NC]
RewriteRule !^admin/system/ /admin/system%{REQUEST_URI} [L]

RewriteCond %{HTTP_HOST} !^www\.mydom\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^/.]+)\.mydom\.com$ [NC]
RewriteRule !^subdomains/%1 /subdomains/%1%{REQUEST_URI} [L]

RewriteCond %{HTTP_HOST} !mydom.com$ [NC]
RewriteRule ^(.*)$ http://www.mydom.com/$1 [L,R=301]

it avoids multiple rewrites
But I do not know if it was the source of your problem

Upvotes: 2

Related Questions