How can I stop .htaccess redirecting a subdomain to the main site?

馋奶兔 提交于 2019-12-11 02:16:08

问题


I have a .htaccess question. Basically my .htaccess is redirecting my newly created sub domain, to the main site, and I don't want it to do so. Let's assume my domain is called 'www.beans.com', and the subdomain is 'shop.beans.com', which is in the public_html folder under /shop/. Here is the .htaccess:

DirectoryIndex index.php enmain.php

ErrorDocument 404 /404.html

## EXPIRES CACHING ##

ExpiresActive On
ExpiresByType image/jpg "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType image/ico "access plus 1 year"
ExpiresByType text/css "access plus 1 week"
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType text/x-javascript "access plus 1 week"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType text/xml "access plus 2 hours" 
ExpiresDefault "access plus 1 hour"

## EXPIRES CACHING ##

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

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.(html?|php)(\?[^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]*/)*)index\.(html?|php)$  http://www.beans.com/$1  [R=301,L]
# Start CloudFlare:beans.com rewrite. Do not Edit 
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^beans.com 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 
# End CloudFlare rewrite. 

So in summary, I would like when I browse to 'shop.beans.com' not to be redirected to 'www.beans.com' (which currently happening).

How can I do it?


回答1:


First thing you have 2 rules doing same thing i.e. adding www..

1 - Delete this rule:

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

2 - Then change last rule like this:

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



回答2:


There must be a rule in your .htaccess file in which you set redirect all urls that are opening witout www to with www. you don't need to change this file.

you should do this:

create a .htaccess file in your subdomain and add the following code in it.

RewriteEngine on
RewriteBase /

save this file. Now all the things work properly.



来源:https://stackoverflow.com/questions/19306226/how-can-i-stop-htaccess-redirecting-a-subdomain-to-the-main-site

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