htaccess will not rewrite subdomains

喜夏-厌秋 提交于 2019-12-24 00:51:15

问题


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)


回答1:


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]



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/36745680/htaccess-will-not-rewrite-subdomains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!