How to serve content from different directory for sub domains - php

How to serve contents from different directory for virtual sub domains.
Ex: http://www.website.com gets its content from www directory.
for virtual.company.com i want to serve content from /www/application directory.
And i want to keep the url parameter same.
I have already created php files to display custom message based on the sub-domain., But i am having problems with htaccess,
In simple i just want to load content for subdomains from different directory other than the root directory.

Try this:
RewriteCond %{HTTP_HOST} !^www\.company\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.company\.com$ [NC]
RewriteRule ^(.*)$ /application/%1 [QSA,L]

That doesn´t sound like a very good idea as people could enter http://www.website.com/application and that might lead to unwanted results. I would recommend setting up a virtual host for virtual.company.com and separate the two.
However, it is possible and it would be something like:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^virtual\.company\.com$ [NC]
RewriteRule ^(.*)$ http://www.company.com/application/$1 [R=301,L]

Setup a named virtual host.
<VirtualHost *:80>
ServerName sub.domain.com
DocumentRoot "/path/to/sub/domain"
</VirtualHost>
<VirtualHost *:80>
ServerName another.domain.com
DocumentRoot "/path/to/another/sub/domain"
</VirtualHost>

Related

htaccess redirection to another domain

I am newby in htaccess, please help if you can.
My task is simple, I think.
I have a website, for example newsite.com. It was made on wordpress.
And I have old copy of this site, on other domain, for example oldsite.com.
When I transfered files from oldsite to newsite some of pictures didn't transferred to newsite, but they still exists on oldsite.
Now when pages are loading I checking post thumbnails on newsite, and if they don't exists on newsite, I am replacing newsite url to oldsite url.
But is there any ways to not just replace newsite.com/image.jpg to oldsite.com/image.jpg, but replace newsite.com/image.jpg, to, for example newsite.com/imgs/image.jpg, and in .htaccess file make rule that if url has /imgs/ part, it means that need to look for image in oldsite.com/image.jpg
I think you can do it by below .htaccess code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://NEWDOMAIN.com [R=301,L]
In your new site HTML code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ([^.]+\.(jpe?g|gif|bmp|png))$ http://www.newurl.com/imgs/$1 [R=301,L,NC]
You can put this configuration in the file of virtualhost.
<VirtualHost *:80>
ServerName example.org
DocumentRoot /srv/redireccion
Redirect permanent / https://example2.org/
</VirtualHost>

htaccess invisibly rewrite folder

I have multiple-domains with different content on one server in the directory /html/, which serves all domains, like example1.com, example2.com, example3.com, example4.com.
This works fine.
I now need to have 2 different(!) wordpress-installations. From the outside I would like to have:
example1.com -> No Blog
example2.com/blog/
example3.com -> No Blog
example4.com/blog/
The internal path-structure on the server should look like:
/html/
/blog-for-server2/
/blog-for-server4/
Because otherwise the content-management-system in /html/ gets messed up. In fact this is almost like having independent subdomains for the blogs and pointing them to different pathes on the server (but I don't want to use subdomains).
Any ideas?
Assuming you've already created the domains in Apache's httpd-vhosts.conf file you can create the redirects inside that, something like:
<VirtualHost *:80>
ServerName example1.com
DocumentRoot /home/servers/common_root/html
RewriteEngine On
RewriteRule ^/blog/? /blog-for-server-1
</VirtualHost>
<VirtualHost *:80>
ServerName example2.com
DocumentRoot /home/servers/common_root/html
RewriteEngine On
RewriteRule ^/blog/? /blog-for-server-2
</VirtualHost>
Otherwise you can do it in a root .htaccess but it's messy:
RewriteEngine On
RewriteCond %{SERVER_NAME} =example1.com
RewriteRule ^blog /blog-for-server-1
RewriteCond %{SERVER_NAME} =example2.com
RewriteRule ^blog /blog-for-server-2

Redirect different domains pointing to same server to different version of same website

I have different domains say like (fictious names) "plumbers.org" and "cleaning.org"
pointing to the same folder in my apache2 conf.
and the main domain is "workers.org"
what i want to obtain is basically to have plumbers.org pointing to workers.org/index.php?version=1
and to have cleaning.org pointing to workers.org/index.php?version=2
is something I set in my .htacess? what's best practice?
You can try to add a permanent redirect in the original httpd / apache conf file, e.g.:
<VirtualHost ...>
ServerName plumbers.org
Redirect 301 / http://workers.org/index.php?version=1
</VirtualHost>
...
<VirtualHost ...>
ServerName cleaning.org
Redirect 301 / http://workers.org/index.php?version=2
</VirtualHost>
Technically, it should also work if you put these changes in the corresponding .htaccess files (if .htaccess files are not prohibited by the "parent" conf). Without <VirtualHost> section, of course, then.
Assuming all 3 domains point to the same DocumentRoot folder, you can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)plumbers\.org$ [NC]
RewriteRule ^/?$ index.php?version=1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(?:www\.)cleaning\.org$ [NC]
RewriteRule ^/?$ index.php?version=2 [L,QSA]

mod_rewrite rules to force www and https over main domain, ignoring 1 folder and 1 other domain

I've tried tons (probably in excess of 250) different rewrite rules but none of them are having the desired effect.
Rules for maindomain.com which is a wordpress website with SSL & should run with the prefix www.
Check and force www. to the entire domain/folders/subfolders...etc (not just wordpress) EXCEPT the folder testfolder
Check and force https. to the entire domain/folders/subfolders...etc (not just wordpress) EXCEPT the folder testfolder
Rules for test.otherwebsite.com which is a custom set of pages hosted in /testfolder/ on the server
Check and forcefully remove www. from the domain test.otherwebsite.com
Check and forcefully changed https to http from the domain test.otherwebsite.com
These rules shouldn't be needed I don't think... if the first set of rules work correctly.
No need to use rewrite engine if you have access to main configuration files.
(1) Redirect non www to www. A simpler solution is using VirtualHost directive instead of rewrite engine.
<VirtualHost *:80>
ServerName maindomain.com
Redirect permanent / http://www.maindomain.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.maindomain.com
# ... Your www served here
</VirtualHost>
For https, you could just simply copy them and replace the port to 443.
(2) Serve /testfolder in a separate VirtualHost directive
<VirtualHost *:80>
ServerName test.otherwebsite.com
# ... anything else
</VirtualHost>
For https, change port to 443.
Note that those won't make exception to /testfolder if accessed from www.maindomain.com/testfolder.
A solution is to move the testfolder outside directory served by www.maindomain.com. For example
/var/www/maindomain <--- directory for www.maindomain.com
/var/www/testfolder <--- directory for test.otherwebsite.com
There is a <DirectoryMatch> directive to prevent that as explained here https://stackoverflow.com/a/214908/779320. But I'm afraid it apply globally and so can't be served in any VirtualHost. But you can test it anyway.
1) You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule !^testfolder/ https://www.maindomain.com%{REQUEST_URI} [NE,NC,R=301,L]
2) Then you can use this code in your /testfolder/.htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://test.otherwebsite.com%{REQUEST_URI} [NE,R=301,L]

.htaccess subdomain redirection to root domain

i just want to ask a quick question about .htaccess.
Here's how my webhosting works with subdomains...
Once i create a subdomain... then they create a folder into the root folder like this...
www.mydomain.com ---> public_html
sub.mydomain.com ---> public_html/sub
What i want to do is... to redirect all request from sub.mydomain.com to www.mydomain.com with some GET variable or something to identify from what subdomain the request is coming from...
So for example... when i get a requests to work like this
http://sub.mydomain.com/myphp.php ---> http://www.mydomain.com/myphp.php?comingfrom=sub
http://sub.mydomain.com/(anyUrl) ---> http://www.mydomain.com/(anyUrl)?comingfrom=sub
I'm also wondering if this would execute some .htaccess redirects present in the main domain...
Hope you guys could help me...
Thanks in advance...
Put a .htaccess with following content into your subdomain folders:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^(.*) http://www.mydomain.com$1?comingfrom=%1 [QSA,R=301,L]
(untested, sorry)
EDIT:
you pointed out that you would like to keep the subdomain in your addressbar and don't want a redirect. So you need to make a view changes to your <VirtualHost> of www.mydomain.com like so
<VirtualHost ...:80>
ServerName www.mydomain.com
ServerAlias mydomain.com
ServerAlias sub.mydomain.com
DocumentRoot /path/to/your/docroot/of/www.mydomain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^(.*) $1?comingfrom=%1 [QSA,PT,L]
</VirtualHost>

Categories