How to Rewrite Domains to folders, by apache mod_rewrite?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eurusd
Forum Newbie
Posts: 8
Joined: Sat May 02, 2009 5:56 pm

How to Rewrite Domains to folders, by apache mod_rewrite?

Post by eurusd »

How to Rewrite Domains to folders, by apache mod_rewrite?
I have got many domains like this:
Mybestdomain.com
Londondomen.co.uk
Primedomainame.info
Promotion.television.org
Blogging.mysite.com
News.freedomains.co.uk
Tv.news.blogging.net
Internet.news.blogging.net
….
Etc…..
They are all pointing to one ip and to one folder
/home/www/allinone/
All these domains take many files from folder
/home/www/allinone/oftenusedfiles/
/home/www/allinone/oftenusedfiles2/
/home/www/allinone/oftenusedfiles3/
/home/www/allinone/usedfilesabc/
and from /home/www/allinone
$root= _SERVER["DOCUMENT_ROOT"];

by include ( $root.”/ oftenusedfiles/usedfunction.inc”);
…… etc ………..



How to do this:
When I enter domain in my browser lets say
http://Mybestdomain.com/ it shows http://Mybestdomain.com/ but goes to
/home/www/allinone/ Mybestdomain.com
Or when I enter domains in my browser for example:
http:// Londondomen.co.uk
it shows same URL but fetches data from Londondomen.co.uk
Basically like this domain name becomes a folder with according name

http://%{HOST_NAME}/ --------- /home/www/allinone/%{HOST_NAME}/

http://%{HOST_NAME}/script.php?show=45 --------- /home/www/allinone/%{HOST_NAME}/script.php?show=45

http://%{HOST_NAME}/%{REQUEST_URI%{QUERY_STRING} --------- /home/www/allinone/%{HOST_NAME}/%{REQUEST_URI}%{QUERY_STRING}


http:// Blogging.mysite.com gets data from /home/www/allinone/ Blogging.mysite.com/
http:// Promotion.television.org gets data from /home/www/allinone/ Promotion.television.org/
….
/home/www/allinone/ Londondomen.co.uk/
/home/www/allinone/ News.freedomains.co.uk/
/home/www/allinone/ Internet.news.blogging.net/

Tried all I could nothing works, I really don’t understand reg exp and rewrite

RewriteCond %{HTTP_HOST} ^(.+)$

RewriteRule (.+) /%{HTTP_HOST}/ [QSA,L]

RewriteRule /%{HTTP_HOST}/%{REQUEST_URI} [QSA,L]


Etc…

THANK YOU FOR YOUR HELP!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to Rewrite Domains to folders, by apache mod_rewrite?

Post by requinix »

How is this all set up? Because I'm sure you can be doing it differently.

Like with VirtualHosts.
eurusd
Forum Newbie
Posts: 8
Joined: Sat May 02, 2009 5:56 pm

Re: How to Rewrite Domains to folders, by apache mod_rewrite?

Post by eurusd »

what do you mean vhosts?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to Rewrite Domains to folders, by apache mod_rewrite?

Post by requinix »

Uh huh. Explains a lot.

You can have Apache serve up multiple websites on one machine: VirtualHosts is how.
eurusd
Forum Newbie
Posts: 8
Joined: Sat May 02, 2009 5:56 pm

Re: How to Rewrite Domains to folders, by apache mod_rewrite?

Post by eurusd »

but i want one document root for all of them
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to Rewrite Domains to folders, by apache mod_rewrite?

Post by requinix »

Not according to your first post. Each site has a different document root.

If they don't then why are you trying to get a RewriteRule to accomplish just that?
eurusd
Forum Newbie
Posts: 8
Joined: Sat May 02, 2009 5:56 pm

Re: How to Rewrite Domains to folders, by apache mod_rewrite?

Post by eurusd »

you dont read carefully

and from /home/www/allinone
$root= _SERVER["DOCUMENT_ROOT"];
$root for everyone of these domains is the same /home/www/allinone/

all i am asking is the simple question
how do you rewrite {DOMAINNAME} to get data from $ROOT/{DOMAINNAME}/

it is a very simple questions for those who know mod rewrite
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to Rewrite Domains to folders, by apache mod_rewrite?

Post by requinix »

eurusd wrote:How to do this:
When I enter domain in my browser lets say
http://Mybestdomain.com/ it shows http://Mybestdomain.com/ but goes to
/home/www/allinone/ Mybestdomain.com
Or when I enter domains in my browser for example:
http:// Londondomen.co.uk
it shows same URL but fetches data from Londondomen.co.uk
Funny, I read that to mean
How do I make each of these sites serve out of a different directory?
Which is exactly what VirtualHosts can do.

Stop trying to do what you think is right and start looking for a solution to your real problem.
eurusd
Forum Newbie
Posts: 8
Joined: Sat May 02, 2009 5:56 pm

Re: How to Rewrite Domains to folders, by apache mod_rewrite?

Post by eurusd »

jesus :)

all i am asking forget virtualhosts forget everything
just tell me how via .htaccess rewrite

rewrite any domain to its folder?
it is probably two lines code...

probably what i am asking is too difficult for you and you try to solve the problem by suggesting me virtualhosts
i did not ask you about virtualhosts, i asked about rewrite
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: How to Rewrite Domains to folders, by apache mod_rewrite?

Post by ldougherty »

Hello, a simple example I've used for our customers in the past for rewriting traffic from a subdomain to a subfolder. Say you have test.domain.com and you want it to read from domain.com/test

Create a .htaccess file in your web root with the below information or place this information in your httpd.conf file.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^test.domain.com$
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ /test/$1

Hope this helps out.
eurusd
Forum Newbie
Posts: 8
Joined: Sat May 02, 2009 5:56 pm

Re: How to Rewrite Domains to folders, by apache mod_rewrite?

Post by eurusd »

thank you very much,ldougherty, you are the only one who understands something :lol: !
found extra examples, might be useful from http://forum.powweb.com/showthread.php?t=73248

and now I use like this:

RewriteBase /
RewriteCond %{HTTP_HOST} ^%{HTTP_HOST}$ [NC,OR]
RewriteCond %{REQUEST_URI} !/%{HTTP_HOST}/
RewriteRule ^(.*)$ /home/ftproot/blockofwebsites/%{HTTP_HOST}/$1 [L]


without RewriteCond %{REQUEST_URI} !/%{HTTP_HOST}/
i went into loops and apache says exceeded maximum redirects
although i could see in error.log with debuglevel rewrite 9 that it does loops but could not understand why it did so
domain name was multiplied many times and translated into REQUEST_URI
now any domain can be translated to its sub folder, that's what i wanted

thanks!
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: How to Rewrite Domains to folders, by apache mod_rewrite?

Post by ldougherty »

No problem, glad I could help out.
eurusd
Forum Newbie
Posts: 8
Joined: Sat May 02, 2009 5:56 pm

Re: How to Rewrite Domains to folders, by apache mod_rewrite?

Post by eurusd »

sure you did, appreciate effort ;) :drunk:
Post Reply