Create sub domain using only htaccess? - php

I want a way to create subdomain using htaccess or any thing to do that.
I can create subdomain with change some codes in httpd.conf, but the problem I have only a host now and I can't edit httpd.conf!

You can't. The DNS name server must resolve the subdomain to the correct IP, and Apache (or whatever HTTP server you're using) must set the correct DocumentRoot for the subdomain.
Unless those two are configured properly, the request won't get to you and the htaccess would have no effect.

Related

How to handle incoming CNAME records and point to the specified URL?

I have looked everywhere for something on this but can't seem to find anything.
After setting up the CNAME record of a domain (i.e. user_domain.com) to point to my own example.com, what would be the next step? How would I get it to point to example.com?
I believe that I will need to do something on my end in order to handle these domains but I can't find any tutorials on how to do so.
The error that is received when visiting user_domain.com is:
user_domain.com’s server DNS address could not be found.
For the record, I am using Ubuntu, Apache and PHP. Would something need to be done to the .htaccess file?
CNAME
www example.com
Overall
I am trying to let users point their own domain to example.com
What would be the next step after setting up the CNAME record?
I would appreciate any help, thanks!
You either have to:
Configure your Apache with name-based virtual hosts and have it listen explicitly for the new domain via the ServerAlias directive.
Or:
Configure your Apache for IP-based hosts. It will serve the example.com content for whatever domain name is pointed at it.
Or:
Configure your Apache with name-based virtual hosts and have it listen implicitly for the new domain by making sure example.com is the first vhost listed in the config. This will make example.com the "default" service for IP-based requests. (Not 100% sure about this one, but this is how I recall Apache behaving.)

Redirect and URL Mask all non-existing/existing subdomains to homepage

A website example.com has one subdomain a.example.com. The point here is I want to redirect all existing and non existing subdomain (ex- b.example.com) to example.com without changing URL(URL MASK). To explain it more further, when a user enters b.example.com he must see example.com on his screen but URL must not change from b.example.com --> example.com. I think its possible from .htaccess file but I failed to achieve it.
Do I need to configure virtual host. Since I only have access to .htacces, I wish I can get it done
You need to have two things configured correctly:
A DNS entry with wildcard (*.example.com -> your server IP)
A virtual host with wildcard alias (ServerAlias *.example.com)
Then there is nothing to do in .htaccess. .htaccess alone can't do what you want. And please note also that this could be SEO-toxic (duplicate content), but it depends on your use case.
As you do not have access to virutal hosts config, may be you could go with subfolders instead of subdomains.

How to create dynamic subdomain using PHP and htaccess?

I have one problem:
I want to set up PHP and htaccess to make dynamic subdomains. And I can not figure out how.
Currently my URL looks like this:
www.exemple.com/index.php?subdomain=mike&component=content&id=26&title=people-love-apps
I need it to look like this:
www.mike.exemple.com/content/26/people-love-apps.html
I know it can be done but I do not know a solution how to do it.
Is very important to me with $ _GET [] functions to read parameter in the URL.
The problem is that users on my site make their web site and get a free domain (sub-domain) automatically. This should be automatic.
Thanks!
You can't make dynamic subdomains with .htaccess
You will need to configure the apache virtual host to accept requests for multiple domains
See the Apache documentation - Using Name Based Virtual Hosts
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com *.example.com
DocumentRoot /www/domain
</VirtualHost>
Adding the wildcard subdomain *.example.com, your PHP application will receive all requests for any domain below example.com, ie garbage.example.com, busted.example.com, llama.example.com, etc.
Creating Wildcard Sub Domain Using Apache VirtualHost
Multiple Domains to One Virtual Host | Wildcard Host (shared hosting)?
Apache default virtual host for multiple domains
At this point, your application will have to determine the validity of the subdomain and display the appropriate error for unknown subs.
From there, parse the domain for mike.
OF COURSE you can interprete dynamic subdomains with htaccess.
The following rule will solve the problem.
RewriteCond %{HTTP_HOST} www\.([^.]+)\.example\.com [NC]
RewriteRule ^/content/([0-9]+)/([a-zA-Z0-9_\-])\.html /index.php?subdomain=%1&component=content&id=$1&title=$2 [L]
so anyone visting
www.mike.exemple.com/content/26/people-love-apps.html
will see the content of
www.exemple.com/index.php?subdomain=mike&component=content&id=26&title=people-love-apps

OpenScholar virtual subdomain

I installed OpenScholar in my domain:
http://scholar.web
Basically, when someone register for an account, his site URL is http://scholar.web/user, and his content will be at http://scholar.web/user/contents
I've been searching a way so that the URL is converted into: http://user.scholar.web/content for displaying his content via virtual subdomain (htaccess maybe)
Anyone can provide some solutions or guidance?
thanks
First of all, you'll need to change the DNS entry on the domain so that *.scholar.web points to your site.
Afterwards, (If you're using Apache) change the Apache vhost to accept *.scholar.web requests.
You can then use htaccess and/or PHP to do what you like with the URL formatting.
I used to have a domain called something along the lines of "iscool.com".
This is the method I used so I could use pseudo subdomains like "ben.iscool.com" and it would display content accordingly.

Dynamic .htaccess subdomain - php

I am facing small issuse with .htaccess and subdomain. To give some information, i am developing some project in php. And need to have url handled this way
My application user put their name as myname.domainname.com
they put myname.domainname.com then still i need to keep that url as it and need to call my internal file to process their data. example: i need to call myphpfile.php or any other file that i am using in application. so when any file is called from the url then i want to retrive subdomain name so that i will able to get related data
Let me know if somebody provide me simple solution. i have developed many files , now i got stucked to achieve the results.
Create a wildcard DNS entry for *.domainname.com
Create a wildcard virtual host for *.domainname.com
<VirtualHost *:80>
ServerName *.domainname.com
DocumentRoot /var/www/html
</VirtualHost>
All requests to all subdomains will go to the same document root, the same set of PHP scripts of yours.
$_SERVER['SERVER_NAME'] gives the whole host name (e.g. myname.domainname.com). If you only want the subdomain part, use:
$subdomain = preg_replace('#\..*$#', '', $_SERVER['SERVER_NAME']);

Categories