.htaccess subdomain redirection to root domain - php

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>

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>

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]

Redirect from subdomain to primary domain without rewriting URL

I've been searching similar solutions but none of them seems working on my server.
What I'm trying to do is set redirect from yyy.zzz.com (subdomain) to zzz.com (primary domain) without rewriting url. So both yyy.zzz.com and zzz.com actually pointing at same directory and same files in it.
Currently I have this .htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^yyy\.zzz\.com$ [NC]
RewriteRule ^(.*)$ http://zzz.com [L,NC]
Which, of course, just redirects straight on, and url actually changes.
Just for information, I want to set such subdomain url for CMS, so if user wants to enter CMS he does it from subdomain, while actually only $_SERVER['HTTP_HOST'] is changing.
Any help would be appreciated.
You should set up a ServerAlias in your apache config. Setting yyy.zzz.com as an alias for zzz.com
<VirtualHost *>
ServerName zzz.com
ServerAlias yyy.zzz.com
# ...
</VirtualHost>
http://httpd.apache.org/docs/2.2/mod/core.html#serveralias

Redirect Url in codeigniter

I want to redirect a url in my lcalhost ,as when user types : mystring.localhost/CI to localhost/CI/Mycontroller/Myaction/mystring in codeigniter .its now showing Server not found and says browser Firefox can't find the server at www.mystring.localhost. How to do this ? Thanks in advance. I have this in my .htaccess :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mystring\.localhost\CI\
RewriteRule ^(.*)$ http://localhost/CI/Mycontroller/myaction/mystring/$1 [R=301]
On Windows edit the file C:\Windows\System32\drivers\etc\hosts and then append:
127.0.0.1 mystring.localhost
So it will redirect all requests from mystring.localhost to 127.0.0.1
On Linux is the same syntax but file is at /etc/hosts
And then, you must make some Alias or RewriteRule with RewriteCond on Apache configuration or .htaccess.
add to c:\wamp\bin\apache\ApacheYOUVERSION\conf\extra\httpd-vhosts.conf
NameVirtualHost *.yourdomain.dev
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/CI"
ServerName *.yourdomain.dev
</VirtualHost>
Find #Include conf/extra/httpd-vhosts.conf in c:\wamp\bin\apache\ApacheYOUVERSION\conf\httpd.conf and delete # at the beginning
in your hosts file: 127.0.0.1 yourdomain.dev
add this to your htaccess:
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).yourdomain.dev [NC]
RewriteRule ([a-z0-9-]+)(/+.*) index.php/$1/%1$2 [QSA]
So if I understand correctly:
-Someone would type in mystring.localhost/CI, and you would like it to redirect them to a certain page with an action and value?
If so, you can set a page as the index page in \application\config\config.php
Its on line 30:
$config['index_page'] = 'Mycontroller/Myaction/mystring';
Otherwise in the controller that handles your index just do a redirect in it's __construct function.
There is most likely a solution involving the HTACCESS file but I am not very knowledgeable on that.
Hope this helps!
-Gui

How to serve content from different directory for sub domains

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>

Categories