make all subdomains access website root files - .htaccess

Here's what I want to do:
Let's say i have www.mysite.com and it's a complex website with alot of files
I want to make fr.mysite.com, it.mysite.com, uk.mysite.com to access the file in the root website (ie: fr.mysite.com/jobs.php will actually load mysite.com/jobs.php but in the browser url it will show the link with subdomain).
I want to build a site with content from multiple countries and I want each country to be accessed with it's code as a subdomain. If I can do that I can then process the url in php and know what country code it's in the url.
Copying the entire site to each subdomain folder isn't an option.
Let me know if you have any idea on how to do that, I guess it's a .htaccess thingy but I can't figure it out .

Use ServerAlias in your VistualHost configuration: set it up as single virtual host where all sub-domains point into the same root folder:
<VirtualHost *:80>
ServerName mysite.com
ServerAlias *.mysite.com
DocumentRoot "\path\to\your\site\"
...
</VirtualHost>
This is the most recommended way -- no need to involve URL rewrite here.

Related

How to access to site by 2 address

I have a site now with address https://example.com/. How to make possible access to this site also by address https://example.com.html? What to write in htaccess?
What to write in htaccess?
Nothing special.
The question is not what to write but what to not write in .htaccess. And the answer is: do not write anything that links the site to one of its names (example.com or example.com.html). The same applies to the PHP source or configuration files.
The rest is just proper names registration, nameserver and Apache configuration.
Regarding the configuration of Apache, let's say the IP address of your web server is 1.2.3.4. The Apache configuration file (httpd.conf f.e.) now contains something similar to this:
<VirtualHost 1.2.3.4:80>
ServerName example.com
DocumentRoot "/www/docs/example.com"
# Other configuration directives for domain example.com
</VirtualHost>
In order to let Apache know the domain example.com.html is the same as example.com you can add a ServerAlias to the <VirtualHost> block:
<VirtualHost 1.2.3.4:80>
ServerName example.com
ServerAlias example.com.html
DocumentRoot "/www/docs/example.com"
# Other configuration directives for domain example.com
</VirtualHost>
Assuming you already have the name and the name servers are properly configured to point to your existing server, restart Apache and it should work.
Read more about <VirtualHost> and ServerAlias

Creating subdomain aliases

I have a server with Plesk 10.4 on CentOS6, which has an interface for creating aliases of a domain, but not aliases of a subdomain.
For example I have this subdomain set up:
test.my-domain.co.uk
And then I want to create aliases so that the following subdomains map to the same root:
alias-1.my-domain.co.uk
alias-2.my-domain.co.uk
alias-3.my-domain.co.uk
I've tried creating a vhost.conf file like this:
<Directory /var/www/vhosts/my-domain.co.uk/subdomains/test>
# aliases
ServerAlias alias-1.my-domain.co.uk
ServerAlias alias-2.my-domain.co.uk
ServerAlias alias-3.my-domain.co.uk
</Directory>
And then reconfiguring all domains:
/usr/local/psa/admin/sbin/httpdmng --reconfigure-all
However that fails.
Is this possible? Am I doing something wrong?
I suggest to
copy files and related data of subdomain to somewhere
delete subdomain
create add-on domain test.my-domain.co.uk
Now you should have ability to create aliases for test.my-domain.co.uk.
[update]
You are able create subdomain test.my-domain.co.uk and add-on domain test.my-domain.co.uk.
The difference in fact that for add-on domain you will have ability to create domain aliases.

Domain that shows another domains subdirectory

Is it possible to have e.g. "domain.com" that shows "domain2.com/dir".
What I mean is, that instead of domain.com just redirecting, I want it to show e.g. "domain2.com/dir/subdir" as "domain.com/subdir"
All this is regarding to a CMS.
What I need is, that my frontend to my CMS is at domain.com. A customer is creating his account with a shorttag. E.g. "domain2.com". Then their unique URL to THEIR frontend is "http://domain.com/domain2.com".
What I need is, that domain2.com can show "http://domain.com/domain2.com" - so that the customers doesn't need to redirect their users to http://domain.com/domain2.com, but can simply just redirect to their own domain - domain2.com
Thanks in advance.
If you're using cPanel you can set up an addon domain that can point to a specific directory.
If you're just using apache you'll need to point the domain to another folder in the vhosts.
<VirtualHost *:80>
DocumentRoot "/home/user/domain.com"
ServerName domain.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/home/user/domain.com/dir"
ServerName domain2.com
</VirtualHost>
you can use http header in your first page(domain2.com/dir/subdir) to redirect to second page
in php this is something like this (in index.php file in subdir):
<?php
session_start();
header('Location: http://domain.com/dir');
die();
?>

How Change Subdomain Root Folder to Public_html via .htaccess?

I have a subdomain that I want to make the root folder to "Public_html",
for example, my root subdomain is
- /public_html/subdomain
So I want to change that path to just /public_html/ for my subdomain. I know it can be done with .htaccess, but I dont know how to do it. Thanks...
I don't know exactly what you want, but here are my two answers:
Answer 1: You want your DocumentRoot to point to public_html
In this case add/change the DocumentRoot in your (virtual) host config of your webserver (I'm assuming you're using Apache, which would be /etc/httpd/conf/httpd.conf or /etc/httpd/conf/extra/httpd-vhosts.conf or even /etc/apache2/sites-enabled/yourhost)
Answer 2: You want to redirect from your subdomain folder to the root of public_html
This is not possible via a simple RewriteRule due to the fact, that your host is pointing to the subdomain folder. The other way round would possible (redirecting from public_html to subdomain).
What you can to is to create a symbolic link from subdomain which points to public_html or a file in it - but I don't recommend this.
Furthermore resources:
A brief description on how to forward to a subfolder using Rewrite rules
Another brief description about the DocumentRoot

redirecting without changing URL

i want to redirect domain domain1.com to domain2.com completely but without changing domain1.com URL.
i thought i could do that with .htaccess but i found out that it is not possible because they are to different domains and i should use http:// in .htaccess rule so it would be an external rewriting not an internal rewriting,so the URL will change.
is there any other solution? is using iframe the only solution?
i have to add that i don't want to change DNS setting for these 2 domains.
If both domains point at the same server then you can setup your apache config to point both domains at the same document root.
NameVirtualHost *
<VirtualHost *>
ServerName domain1.com
DocumentRoot /www/mysite
</VirtualHost>
<VirtualHost *>
ServerName domain2.com
DocumentRoot /www/mysite
</VirtualHost>
However, I would recommend domain2.com redirects (with a 302 redirect in the .htaccess) because it will improve your search engine optimisation, as both sites will be considered as one. So if a GoogleBot finds domain2.com as a link in another site, it will add it as a pagerank to domain1.com.
If they run on the same webserver you could set domain2 as an ServerAlias for domain1.
I thought I once read you could give a flag to mod rewrite that it internally proxies the request to another domain, however I have never used this myself (if it even exists) and I can't honestly say I think it'll be great for performance.. (but maybe it is..)
But why would you want two different domains show the exact same website, without changing the hostname to, what you see as, the primary one. If I might ask?

Resources