简体   繁体   中英

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

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?

First thing you have 2 rules doing same thing ie 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] 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM