Forum Moderators: phranque

Message Too Old, No Replies

.htacces troubles: changing document root of subdomain

         

burley

10:52 pm on May 4, 2012 (gmt 0)

10+ Year Member



Hi all, i need to change the document root of a subdomain through htacces.

I searched but can't seem to find the correct code (apparently there are no priors) and am not able to produce the code myself.

I found (or got pointed to it by my host in an effort to provide service), but can't seem to get it to work. I'm not even sure it could work;



Options -Indexes +FollowSymLinks

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

RedirectMatch 301 ^/subdomein2/(.*)$ http://subdomein2.domein.nl/$1


Anyway, your help is appreciated!

g1smd

11:42 pm on May 4, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is this going in htaccess or in the main config file?

There are a multitude of issues with that example code.

Use example.com in this forum.

burley

12:00 am on May 5, 2012 (gmt 0)

10+ Year Member



Thanks for your quick reply.

Thats one of my questions, following logic I think the code should be in the 2nd subdomain. But again I don't have (enough) knowledge on this.


Options -Indexes +FollowSymLinks

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

RedirectMatch 301 ^/subdomain2/(.*)$ [subdomain2.example.com...]

lucy24

2:57 am on May 5, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The OP referred to a host, which I assume means we're talking about shared hosting and therefore an htaccess file.

Any code that affects more than one subdomain has to be in a directory where the request will "see" it. That probably means your top-level directory, assuming that your subdomains live in directories inside of the first directory.

Now, please step back and set aside the code for a moment. Explain in English (I don't read technical Dutch) what you want to do. That is: what do the original requests look like? What do you want to change them to?

Use example.com (or, if you prefer, example.nl) in all your examples. This will prevent them from turning into clickable links:

http://www.example.com
http://www.example.nl
[domain.com...]
[domain.nl...]

See the difference? :) In the Apache forum it is very important to see exactly what you typed.

phranque

4:24 am on May 5, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, burley!

2 things to consider at a higher level:

- RedirectMatch is a mod_alias directive and RewriteRule is a mod_rewrite directive and you should never mix those modules' directives.
if you need to use RewriteRule for anything use RewriteRule for everything.

- note the apache documentation on Per-directory Rewrites:
* When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution encounters the end of a rule set. See the RewriteBase directive for more information regarding what prefix will be added back to relative substitutions.
* The removed prefix always ends with a slash, meaning the matching occurs against a string which never has a leading slash. Therefore, a Pattern with ^/ never matches in per-directory context.

http://httpd.apache.org/docs/current/mod/mod_rewrite.html [httpd.apache.org]

burley

8:45 am on May 5, 2012 (gmt 0)

10+ Year Member



Thanks for all your replies!

@ Lucy, I already did that in my second post, did I not?

Perhaps I shouldn't have posted the code, but anyway. I think Lucy is correct and I'm on a shared host. However my subdomains aren't in the same dir as the files for the main dir;

public_html
www
subdomain1
subdomain2

Anyway, without trying to create code because I'm not able to, below is what I need to do;

I need to change the document root of subdomain2.example.com, so that it is pointing to subdomain1.example.com. That's "all".

The reason for this is that I have two webshops, the one in subdomain1.example.com being the original one and the one in subdomain2.example.com being a cloned one. When a customer go's to subdomain2.example.com and is redirected to subdomain1.example.com the shop will automatically show the cloned webshop.

I hope I made clear what I meant and what I'm trying to accomplish.

Thanks for your time and efforts