Sub Domain Name Creation in .htaccess - php

I have an application on SAAS basis where I will be adding clients from backend or the clients can even create their own panel by signing up
While signing up or adding the client , the client or I will be assigning the sub domain . So suppose I am assigning subdomain xyz to client1 . Then the access url will be
xyz.maindomain.com
Now , I want .htaccess code in such a way that if xyz.maindomain.com is called, the url called must be maindomain.com?client=xyz .
Please guide me how to do this.
Thanks in advance.

We could use mod_rewrite to achieve what you wanted. No server-side scripting such as PHP required.
Say, put this in .htaccess file on your root www directory (or any relevant Apache *.conf file if you wish)
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/?client=%1/$1 [L,R=301]
This will redirect foo.example.com to http://www.example.com/?client=foo
(change example.com to your domain name)
More about mod rewrite, with examples:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708
Hope this help.

Related

.htaccess 2 domains, same target, 1 redirect

I own 2 domains that are pointing to the same static ip. I wanted to set domain1.com to be redirected to domain1.com/down.php but only for the target domain1.com and domain.com/index.php/html.
The domain2.com should work as usual, so no redirects here. It should target /var/www/html like it currently does.
Background is: that there was a project which should not be available on the domain1.com, but there are specific files such as domain1.com/subdir/subdir/file1.jpg, which should still be accessible there.
Any ideas?
Many Thanks
.htaccess to redirect domain1.com/*
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ down.php?_url=/$1 [QSA,L]
The rule only works if the condition is meet. ?_url=/$1 is not needed but may prove useful.
If you use htAccess, also it seems you use Apache2
You can also use virtual host directly from apache 2 configuration :
https://httpd.apache.org/docs/current/vhosts/examples.html

How to Chnage root path when we access from mobile .htaccess?

Actually I have two sources
Desktop Layout Support (/Public_html/Desktop) (example.com)
Mobile Layout support (/Public_html/Mobile) (example.com)
How can i link root path based on device access without chnage/redirect domain in .htaccess.
Actually i Have tried
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sample.in$ [NC,OR]
RewriteCond %{HTTP_HOST} ^sample.in$
RewriteCond %{REQUEST_URI} !Public_html/Desktop/
RewriteRule (.*) /Public_html/Desktop/$1 [L]
But i can't able to find device
Please help me to solve my problem.
My suggestion here that you can try to create loader.php located in /Public_html/loader.php set it as root in .htaccess and when request comes to loader.php detect device for example by method suggested here - Simplest way to detect a mobile device
after detection you have 2 options to do
1) Easy option: redirect page to example.com/desctop or to example.com/mobile
2) Hard option: include nigher /Public_html/Desktop/index.php nigher /Public_html/Mobile/index.php which for sure requires a lot additional configuration of include pats in your both projects

Redirect without changing Url htaccess

I'm wondering if this is possible - I have projects structured like:
www.mydomain.com points to MyFolder on server. In MyFolder i have 3 App Folders eg MyFolder/App1, MyFolder/App2..
Each of these are 3 different web applications. Url of domain is www.mydomain.com
Is it possible, that, when url user go to mydomain.com, it automatically redirect to mydomain.com/App1 but without url change visible to user (in browser it will still be mydomain.com)?
I can not point domain to MyFolder/App1 because that way app2 is not accessible.
My solution is that app1 becomes a root which contains app2,app3 etc folder but thats just to messy
edit: question clarification
You should be able to use this code here.
RewriteEngine on
#ignore App1 and App2 and interally Rewrite to /App
RewriteCond %{REQUEST_URI} !(App1|App2) [NC]
RewriteRule ^(.*)$ /App/$1 [L]
Yes, you should be able to do it in your .htaccess file like this (untested code):
RewriteCond %{HTTP_HOST} ^example.com/App
RewriteRule ^(.*) http://www.example.com/App1 [P]

Create a Subdomain in godaddy hosting using PHP

Is there any easiest way to create a sub domain using PHP code on godaddy.com hosting?
e.g:
jon.mywebsite.com
jessie.mywebsite.com
etc
you need to create a A record to serve all your subdomains
*.your-site.com IN A YOUR-IP-ADDRESS
then you need to create a .htaccess file and
RewriteCond {REQUEST_URI} !\.(png|gif|jpg)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?uri=$1&hostName=%{HTTP_HOST}
this will ignore images (SEO friendly URLs).
Now you can redirect your users to $userName.your-site.com
Alternatively try this:
setup your application so your users goes to
your-site.com/user
your .htaccess should look like this
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([aA-zZ])$ index.php?username=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.mywebsite.com
RewriteRule (.*) index.php?username=%1
so now when you hit the index.php it will grab the user and redirect to $user.your-site.com as a custom subdomain. (in this case usernames are limited to a-z characters)
I dont think with PHP Code you can but with the help of .htaccess i think it is possible.
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+jon/
RewriteRule ^jon/(.*)$ http://jon.mywebsite.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} \ /+jessie/
RewriteRule ^jessie/(.*)$ http://jessie.mywebsite.com/$1 [L,R=301]
RewriteRule ^(jon|jessie)/ - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?jon\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ /jon/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?jessie\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ /jessie/$1 [L]
You can make subdomain much easier using control panel, why to do it the hard way ?
This is an old question, but in case anyone is looking for the answer. This is specific to GoDaddy, but other Hosts may do something similar to this.
After you setup your DNS A record you need to go to Hosted Domains and add the subdomain folder name under the root domain account.
Go to Hosted Domains.
Under Hosted Domains you will see a root domain table column.
You will also see a Subdomains table column with an "Add" link.
This link is for adding subdomains to a root domain.
Type in/enter the name of the subdomain folder.
Create either an index.php or index.html file and upload it to your subdomain folder and echo something like "Setup Completed". I noticed that if you are doing something like creating a restricted "api" subdomain for example and the GD script cannot access the index file in your subdomain folder then the "process" does not complete until you add a plain index.php or index.html file in your subdomain folder. After that is done then you can add anything you want in that subdomain folder/site.

is it possible to have many sub-domains with the code in the root folder

i am developing a website. where in user would be asked to select their country( like USA, UK, Australia & Canada in the websites landing page.
i am planning to create a sub-domain for each country. Now my query is,
is it possible to run the website with
these 4 sub-domain with the code and
data bases in the main ROOT FOLDER
.
OR
do we need to install the code
separately in all sub-domains and
create the separate Data bases
.
Thanks in advance.
Regards,
Gourav
You can so this with a .htaccess file which redirects to the correct part of the script when they come from any sub domain.
Checkout this page as is has a similar problem and solution you have:
http://blog.gwebtools.com/apache-htaccess-subdomain-redirect-rules/
Extract:
<IfModule mod_rewrite.c>
RewriteEngine On
#redirect gwebtools.com to www.gwebtools.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www..*
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]*).(com|com/)
RewriteRule ^.*$ http://www.%1.%2%{REQUEST_URI} [R=301,L]
#if subdomain pt or whois and folder port-scanner redirect to pt.gwebtools.com/scanner-porta, with parameters
RewriteCond %{HTTP_HOST} ^(pt|whois)\.gwebtools\.com
RewriteRule ^port-scanner/* http://pt.gwebtools.com/scanner-porta$1 [R=301,L]
</IfModule>
Some information about .htaccess and some info so you can understand what the above code does: http://www.webweaver.nu/html-tips/web-redirection.shtml
Or you can do it in php like this tutorial:
http://php4every1.com/tutorials/multi-language-site/
You don't have to create anything seperate, but you would need to include the following in the application:
Method to recognize WHICH subdomain is loaded (en.domain.com) for english language selection, etc.
Create aliases for your domain (or just do a *.domain.com under apache for instance) to funnel the new subdomains into the proper virtual host.
Create content specific to the subdomains (you can have one DB but pull distinct data based on which domain is being used -- again you would need to check for this in your app).
You could add a conditional code that checks $_SERVER["SERVER_NAME"] variable somewhere and set the appropriate database settings based on the variable, without duplicating the code.

Categories