homepage Welcome to WebmasterWorld Guest from 54.145.183.169
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
.htaccess on main domain messing with subfolders
I am having trouble with hosting two domains on the same server.
markds



 
Msg#: 4662335 posted 2:27 pm on Apr 11, 2014 (gmt 0)

Hi all,

I have a very limited knowledge regarding .htaccess, but I have encountered a problem with regards to having multiple domains on a single hosting server.

I have my main domain in the root folder (www.example.com). Apart from this I have three other domains (e.g. www.example2.com) located in subdirectories. It used to work before I changed my .htaccess code, but now they are all automatically redirected to a subdirectory on www.example.com.

An example:
www.example2.com used to work and stay on its own domain www.example2.com without being redirected (with all of the files in a folder on the main domain www.example.com and with the domain www.example2.com pointed to the subdirectory www.example.com/example2/), but is now being directly redirected to www.example.com/example2/. Since the two sites are unrelated, I obviously want to keep them on their own domains both of them.

Can you identify what part of the code is forcing this rewrite, and would you have any idea with regards to have it can be fixed? Can I place a simple .htaccess file in the subdirectory that would solve the problem perhaps?



RewriteEngine On
RewriteBase /

# If there are caps, set HASCAPS to true and skip next rule
RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]

# Skip this entire section if no uppercase letters in requested URL
RewriteRule ![A-Z] - [S=28]

# Replace single occurance of CAP with cap, then process next Rule.
RewriteRule ^([^A]*)A(.*)$ $1a$2
RewriteRule ^([^B]*)B(.*)$ $1b$2
RewriteRule ^([^C]*)C(.*)$ $1c$2
RewriteRule ^([^D]*)D(.*)$ $1d$2
RewriteRule ^([^E]*)E(.*)$ $1e$2
RewriteRule ^([^F]*)F(.*)$ $1f$2
RewriteRule ^([^G]*)G(.*)$ $1g$2
RewriteRule ^([^H]*)H(.*)$ $1h$2
RewriteRule ^([^I]*)I(.*)$ $1i$2
RewriteRule ^([^J]*)J(.*)$ $1j$2
RewriteRule ^([^K]*)K(.*)$ $1k$2
RewriteRule ^([^L]*)L(.*)$ $1l$2
RewriteRule ^([^M]*)M(.*)$ $1m$2
RewriteRule ^([^N]*)N(.*)$ $1n$2
RewriteRule ^([^O]*)O(.*)$ $1o$2
RewriteRule ^([^P]*)P(.*)$ $1p$2
RewriteRule ^([^Q]*)Q(.*)$ $1q$2
RewriteRule ^([^R]*)R(.*)$ $1r$2
RewriteRule ^([^S]*)S(.*)$ $1s$2
RewriteRule ^([^T]*)T(.*)$ $1t$2
RewriteRule ^([^U]*)U(.*)$ $1u$2
RewriteRule ^([^V]*)V(.*)$ $1v$2
RewriteRule ^([^W]*)W(.*)$ $1w$2
RewriteRule ^([^X]*)X(.*)$ $1x$2
RewriteRule ^([^Y]*)Y(.*)$ $1y$2
RewriteRule ^([^Z]*)Z(.*)$ $1z$2


# If there are any uppercase letters, restart at very first RewriteRule in file.
RewriteRule [A-Z] - [N]

RewriteCond %{ENV:HASCAPS} TRUE
RewriteRule ^/?(.*) /$1 [R=301,L]


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^.*blog.*$ [NC]
RewriteCond %{REQUEST_URI} !^.*product.*$ [NC]
RewriteCond %{REQUEST_URI} !^/$ [NC]
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{REQUEST_URI} !^.*blog.*$ [NC]
RewriteCond %{REQUEST_URI} !^.*product.*$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]



RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]


</IfModule>


Thanks a lot in advance!

Best regards,
Mark

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4662335 posted 8:04 pm on Apr 11, 2014 (gmt 0)

Oh, lord.
# Skip this entire section if no uppercase letters in requested URL
RewriteRule ![A-Z] - [S=28]

This is, of course, technically possible. But honestly, unless your name is JDMorgan, or you've got access to the case-changing RewriteMaps, you're safer with a single conditionless

RewriteRule ^([^A-Z]*[A-Z].*)$ /fixup.php [L]

Put this rule after all rules that issue 403s ([F] flag) but before the domain-name and "index.xtn" redirects (because your fixup.php page will include lines dealing with those).

It looks as if you've spliced together htaccess rules from at least two different sources, since there's an <IfModule... envelope smack in the middle of everything.

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Leave a space after each RewriteRule, but don't space within rulesets (between RewriteCond and RewriteRule). This is purely for your own sanity, not for Apache purposes. I had to look at this rule twice to figure out what it's saying: "If the request is for a file that does exist, stop here. Otherwise proceed to the final, conditionless rule." Horribly inefficient, but, uhm, I guess it's not what you asked.

Is there a CMS involved? That last rule sure looks like it.

I don't see anything in the quoted htaccess that mentions subdomains at all. Instead you've got a domain-name-canonicalization redirect that points everything to the primary domain:

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

All that ^.* and .*$ is unnecessary. But if those are directory names, include them. Do the subdomain URLs look like

sub1.example.com/blog/blahblah
or
sub2.example.com/product/blahblah
?

Otherwise I don't see where subdomains would even enter into the picture.

First things last:
www.example2.com used to work and stay on its own domain www.example2.com without being redirected (with all of the files in a folder on the main domain www.example.com and with the domain www.example2.com pointed to the subdirectory www.example.com/example2/), but is now being directly redirected to www.example.com/example2/

That sounds as if a [R=301] flag got appended by mistake. Subdomains obviously have no physical existence, what with the lack of four-dimensional servers. So what I'd expect to see-- but don't see-- is something like

RewriteCond %{HTTP_HOST} sub\.example\.com
RewriteRule (.*) /sub/$1 [L]


Where are those rules? It isn't enough for the subdomain's DNS record to point directly to the relevant /sub/ directory. All requests pass through all htaccess files in their path, so you also need an exception to the
!^(www\.example\.com)?$
redirect.

RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]

Why is this two separate rules? #1 is simply a special case of #2. Incidentally both targets should start in / This is safer than setting a RewriteBase. And where's the subdomain exception? Does each subdomain have the identical filename structure?

markds



 
Msg#: 4662335 posted 8:31 am on May 13, 2014 (gmt 0)

Dear Lucy24,

Thank you so much for all of your help and very detailed answer. It solved the problem straight away and the site is now working perfectly.

Once again thank you.

Best regards,
Mark

markds



 
Msg#: 4662335 posted 8:31 am on May 13, 2014 (gmt 0)

Dear Lucy24,

Thank you so much for all of your help and very detailed answer. It solved the problem straight away and the site is now working perfectly.

Once again thank you.

Best regards,
Mark

markds



 
Msg#: 4662335 posted 8:31 am on May 13, 2014 (gmt 0)

Dear Lucy24,

Thank you so much for all of your help and very detailed answer. It solved the problem straight away and the site is now working perfectly.

Once again thank you.

Best regards,
Mark

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved