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']);
Related
Maybe this question sounds silly or weird to you, but it runs into my head for days so I decided to ask it here.
Basically I'm making a Custom Management System with PHP and because my Website contains a lot of Subdomains, I just wanted to know that is it possible to Manage them within one Admin Control Panel ?
Let's say my Sites looks like this:
www.mysite.com (site 1)
www.subdomain1.mysite.com (site 2)
www.subdomain2.mysite.com (site 3)
And my CMS will be uploaded to the root directory:
www.mysite.com/admin
So from this panel, I have to be able to insert, delete and edit blog posts, images and etc.
So is this possible or I just wasting my time on that ?
Yes. The usual approach is to make your Apache config contain a wildcard match that sends all requests to your application:
<VirtualHost *:80>
DocumentRoot /var/www/example_application/public_html
ServerName www.example.com
ServerAlias *.example.com
</VirtualHost>
And then in the sites code simply grab the subdomain:
$parts = explode( $_SERVER['HTTP_HOST'], '.');
$subdomain = $parts[0];
// A db query to check if this subdomain is valid
// Either set the channel or return 404
I'm sorry if this has been asked before but I can't seem to find a concrete answer. Within my htdocs folder, I created a folder named Testing. Inside Testing, I placed to two php files named: email_form.php and email_script.php.
When I go on my browser(Chrome), I would type in localhost:8888/Testing in the search bar so that I could see it but I can't. What could I possibly be doing wrong?
I had the same issue and found a solution. Keep in mind I am running on windows 10.
The default port was set to 80 and the url for the start page was just localhost/MAMP
I simply changed the default port in preferences to the usual 8888 and then typed localhost:8888/test/index.html to get my test page up and running. I know its an old thread but hopefully this helps someone
If not one of the files is an index file, you need to add the file name in the URL. A common name for index file is index.php, but it depends on your configuration. In this case the file name in the URL is obsolete.
Additional be sure that php files are processed by your webserver.
While entering the filename in the url and the file is not shown (State 404), then check your config (vhost / virtualhost) settings if the correct document folder is used and if there is a name defined, so that the content will be delivered by using a dns name or maybe by a specific IP only.
<VirtualHost *:80>
ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain
</VirtualHost>
So iv'e been trying to find the right path for me for my situation but couldn't just find the right answer.
So what i need to do is a php script that will create a subdomain that redirects to an IP that will be sent from my post request.
Example -
I post to script.php - ?username='test'&ip='127.0.0.1'
I need this post request to create a subdomain with this username(the username will be used for the subdomain - test.mysite.com
visiting test.mysite.com will redirect to 127.0.0.1
What im trying to achive is something like a dns service.
Is it possible? Do i need to ask my host for something(Godaddy/IIS Server)
Thanks ! Really appreciate any lead.
This requires 3 steps:
You need to create a wildcard DNS entry for subdomains of your domain
*.example.com A 192.0.0.10
You then need to setup a new vhost entry for your apache server. This should come after the vhost entry for your existing server, if it's running on the same machine as this subdomain redirection system.
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www
ServerName www.example.com
ServerAlias *.example.com
</VirtualHost>
Finally, you write your index.php script to get $_SERVER['HTTP_HOST'] and based on that, to do a lookup in the database for your subdomain name, and send the header('Location:') based on a successful lookup in the database.
You've probably seen it on websites where they offer a website and you can use your own domain name if you want. So you register a www.website.com/user but you can also set up yourdomain.com to redirect [with A record] to Website_IP_ADDRESS and they handle it because you gave them your domain (www.yourdomain.com) so they know to redirect to www.website.com/user but your address bar remains www.yourdomain.com
krop.com has it
This is what I've been trying to do:
I want to be able to handle incoming redirects from multiple websites and 'fetch' content from a subfolder on the main site but keep the original URL intact.
I want to use php since i want to retrieve the user var (bar and foo) from a db.
Since I won't have access to every domain, I can only play with the incoming part (the blue box)
So far I only had success with duplicating a url (www.bar.com fetches 75.333.444.55) which is pretty useless...
And using file_get_contents('http://address') in my index.php to display subfolder index.html without redirecting again but it's slow and browser unfriendly.
I've been trying to ".htaccess it" with little success
Anything will help
Thanks
You do not want a 302 redirect! You want a virtual host.
Google for virtual host and if you need more help ask - but make sure you tell us what webserver you are using.
Thanks Ariel
here is what happened:
I added the following code to /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
DocumentRoot /var/www/vhosts/website.com/httpdocs/users/foo
ServerName www.foo.com
ServerAlias *.foo.com
</VirtualHost>
AddHandler php-script .php
Works like charm! :)
Don't forget to restart apache to take effect. On mediatemple it is: /etc/init.d/httpd restart
I am developing a site where users will have permalinks for them like user.domain.com or domain.com/user and i want to achieve this by php code. foe ex.
for username stack the permalink is stack and on going to stack.domain.com or domain.com/stack it should go to profile page of user!
Let's split the two cases:
user.domain.com
For this to work, you should first configure your DNS so that *.domain.com points to your server. Then in index.php, you can check if $_SERVER["HTTP_HOST"] matches something.domain.com (using e.g. preg_match). After verifying something is a valid username, you can either display the user's profile page or redirect to the profile.
Caution: make sure that any subdomain you use yourself, like for example www., is not a valid username.
domain.com/user
To implement this, you need to setup some kind of catch-all for non-existing pages. One way would be to instruct your webserver to serve a php-file when it encounters a 404. This file could then use the $_SERVER["REQUEST_URI"] variable to determin if there if a user profile is requested.
Caution: Make sure that any /something that is already a valid page is not a valid username. Alternatively you could use a prefix like domain.com/u/user to be more flexible in the names of your own pages.
you should check if your webserver has those "wildcards", then you could achive this by writting a magic virutal host witch will handle that.
try a virtual host like this:
<VirtualHost *:80>
ServerName local
ServerAlias *.local
VirtualDocumentRoot /var/www/%1/public_html
UseCanonicalName Off
</VirtualHost>
source: http://neziric.org/2010/06/dynamic-apache-vhosts-2/