Load a specified site when visited from certain domain - php

I'm not sure what to search for, but this is what I want to achieve.
I have a domain name registered somewhere. I also have a dedicated Linux server somewhere else. I'm running a website (Apache/PHP) on that Linux server that I can now visit through:
http://[my server ip address]/subfolder-1/index.php
I changed the DNS settings of my domain name to the IP address of my Linux server. Right now when I goto my domain name it actually loads the index.php at: http://[my server ip address].
I know I can do something in Aapache so that it knows when a user comes from www.mydomain.com, that it should display the site located in:
http://[my server ip address]/subfolder-1/index.php
And when I come from www.my-otherdomain.com that it should load the site from:
http://[my server ip address]/subfolder-2/index.php
But I'm not sure what to look for and how this is called? How can I configure my Apache server for that?

You need to setup virtual hosts for each domains on Apache like this :
<VirtualHost *:80>
ServerAdmin admin#test.com
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
after that reload the server :
sudo service apache2 restart

Related

XAMPP won't load index.php when using a domain name

So, when I use localhost/website/index.php to access index.php, it works just fine, yet when I use a domain name like website.com/website/index.php and try to access index.php from another page, it doesn't redirect me there, but instead, reloads the page it was already on.
Then, when I try to access index.php via the search bar, it throws an error saying This page isn't working.
Does anyone know a fix for this?
You need to set up a vhost for Xampp
Here's an example which should be added to C:\xampp\apache\conf\extra\httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin webmaster#website
DocumentRoot "C:/xampp/htdocs/website"
ServerName website.com
ErrorLog "logs/website-error.log"
CustomLog "logs/website-access.log" common
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster#website
DocumentRoot "C:/xampp/htdocs/website"
ServerName website.com
SSLEngine On
SSLCertificateFile "conf/ssl.crt/server.crt"
SSLCertificateKeyFile "conf/ssl.key/server.key"
<Directory "C:/xampp/htdocs/website">
php_flag log_errors on
php_value error_log "logs/website-error.log"
AllowOverride All
Order allow,deny
Allow from all
</Directory>
php_flag log_errors on
php_value error_log "logs/website-error.log"
CustomLog "logs/website-access.log" common
</VirtualHost>
You also need to configure your local hosts file to send requests for website.com to your local IP address.
Your hosts file is here: c:\Windows\System32\Drivers\etc\hosts
Add an entry:
127.0.0.1 website.com
This will allow you to access the website on your local machine.
To access the website on your local network you can either change all computer's hosts files as above, or add a DNS A record into your network switch (if it supports DNS)
A website.com 192.168.1.51 (this is your LAN IP address)
If you need the internet to access the website on Xampp you need to add an A record to your public IP address in your domain name DNS control panel. You can find your pulic IP address here: https://www.whatismyip.com/ and you will also need to allow traffic on ports 80 and 443 in through your router and firewall.
If you do not have a static IP address from your internet service provider, you will need to update your domain name DNS A record each time your IP address changes - e.g. if you restart your router, and/or periodically)
Please note: Xampp was not designed to be accessible from the internet, it will work but it's not secure. Please read https://www.makeuseof.com/reasons-why-you-should-never-use-xampp-on-production-server/

FileNotFound exception Wamp server [duplicate]

So I set up a few virtual hosts with unique urls and they work just fine on the desktop. However, when I connect a mobile device on the network, it can't seem to access anything properly but the default localhost virtualhost and that's only when it's the only virtualhost I have up.
My setup and coding is pretty much this except with a different site title
wamp server 3.0 virtual host on another device
and while that solution redirects me to my unique url, it has a lack of images on a default wordpress website.
Has anyone managed to get mobile devices fully accessing links other than on localhost?
Since I posted the answer you referenced, I have decided upon a simpler solution.
What the actual problem is
Because we cannot fiddle with the configuration of a phone like we can with a PC, the phone can never find the domain name we create in our Virtual Host definition on the Server machine, because it does not exist in any DNS Server for it to locate the IP Address in, and a DNS Server is the only place a phone can look, unless it is jail broke.
If you wanted to access one of your Virtual Hosts domains from another PC you could just add a line like this into the HOSTS file on the other PC like this.
192.168.0.10 example.local
But you cannot do that on a phone/tablet.
What Apache expects to be able to asssociate a request to a Vhost
When we create an Apache Virtual Host, we are actually telling Apache to look at the domain name on the incoming connection and match that domain name to a ServerName that exists in one of our multiple Virtual Hosts definitions.
But if we use for example example.local as our virtually hosted domain when we attempt to connect to that from our phone, the phone does a DNS Lookup and does not find that domain and therefore cannot get its ip address.
The simplest way to get round this is:
Assuming we do not have access to adding record to a DNS Server we have to come up with a different solution.
The simplest of these is to use the IP Address of the PC running the WAMPServer(Apache) server and a specific port number. So thats a different port number for each of our sites we want to use from a phone.
So how do we do this
Add the new listening port to httpd.conf like so after the 2 existing Listen statements
WAMPServer 3: Do this using the menus, not by doing a manual edit on httpd.conf
right click wampmanager-> Tools -> Add listen port for Apache
#Listen 12.34.56.78:80
Listen 0.0.0.0:80
Listen [::0]:80
Listen 0.0.0.0:8000
Listen [::0]:8000
Suggested httpd-vhosts.conf file
#
# Virtual Hosts
#
# Always keep localhost, and always first in the list
# this way a ramdom look at your IP address from an external IP
# maybe a hack, will get told access denied
<VirtualHost *:80>
ServerName localhost
DocumentRoot c:/wamp/www
<Directory "c:/wamp/www/">
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
# The normal Vhost definition for one of our sites
<VirtualHost *:80>
ServerName example.local
DocumentRoot "c:/websrc/example/www"
<Directory "d:/websrc/example/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
# Access example.dev from phone for testing
<VirtualHost *:8000>
ServerName example.local
DocumentRoot "c:/websrc/example/www"
<Directory "d:/websrc/example/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
# assuming yoursubnet is 192.168.0.?
# allow any ip on your WIFI access
Require ip 192.168.0
</Directory>
</VirtualHost>
Restart Apache from wampmanager after completing these edits.
Now you test this from the WAMPServer PC by using the ServerName i.e example.dev and from the phone using the ip of the PC running WAMPServer with the port number i.e. 192.168.0.10:8000
Apache will find the correct code to serve from both requests.
If you want more than one Virtual Host to be accessible from your phone you just duplicate this idea and change the port number for each new site, lets say you would use 8001,8002,8003 etc. For as many sites as you want to access.
You may also have to amend your firewall to allow access on http on port 8000, or whatever port you pick to use

Access vhost via ip address on lan

I am running WAMP and using CodeIgniter for my project and have this on my vhost:
<VirtualHost *:80>
ServerAdmin admin#yahoo.com
DocumentRoot "C:/wamp/www/myproject/assets"
ServerName myproject.dev
ErrorLog "logs/myproject.dev-error.log"
CustomLog "logs/myproject.dev-access.log" common
</VirtualHost>
Now to access this, I added this line on windows/system32/drivers/etc/hosts:
127.0.0.1 myproject.dev
Now for the other computers on the network, I have to edit the hosts file of EACH computer so they can access my virtual host. (yes of course I have to use my ip address instead 127.0.0.1 for other computers)
Now my question is, is there a way that they can access my project by only using my ip address on the browser's address bar like this?
http://192.168.1.112/myproject
I mean there are 100 users that will access that project and it's a big hassle if I edit each one's hosts file. Like adding something to .htaccess, or to the routes of CodeIgniter, or to the <virtualHost>
Note:
By the way, when we are still NOT using Codeigniter (plain PHP codes), this is not a problem. But because of Codeigniter's structure, we can't do it anymore.
Can you just add a DNS entry that points to your IP address and set that as the ServerName that apache responds to?
Alternatively you can do virtual hosting based on IP address and port as described here:
http://httpd.apache.org/docs/2.2/vhosts/ip-based.html
In summary you should be able to do:
<VirtualHost 192.168.1.112:8000>
ServerAdmin admin#yahoo.com
DocumentRoot "C:/wamp/www/myproject/assets"
ServerName myproject.dev
ErrorLog "logs/myproject.dev-error.log"
CustomLog "logs/myproject.dev-access.log" common
</VirtualHost>
And have people access it via
http://192.168.1.112:8000/myproject
But, don't forget to add a Listen directive for port 8000 (or whatever you choose) if you use IP-based Virtual hosts
It might work of you create an alias called /myproject in wamp server and point the document root to 'C:/wamp/www/myproject/assets'
Make sure you have set your wamp server status to online by selecting 'Put Online' in wamp server system tray icon.

Saving 2 PHPSESSID accessing computer using same IP address

Server is WAMP. I have 2 sugarcrm sites, one for production say, saved at www/folder1 , other for development say saved at www/folder2 . I am accessing them using IP address (say 66.102.0.0/folder1 and 66.102.0.0/folder2 ) . Browser saves 1 cookie (PHPSESSID) for both of them. If I login/logout to first site, it effects other site also. I can create subdomain at localhost like folder1.localhost and folder2.localhost but how IP based sub-domain is possible like, folder1.66.102.0.0 and folder2.66.102.0.0 (looks funny :) ) ?Or can I use different port for different folder, like 66.102.0.0:80 and 66.102.0.0:8080 ?Then will browser saved 2 cookies, if port is different on same ip address?I can't change login/logout, it's sugarcrm not core php. I can use different browsers but my boss is saying to make cookies separately. Or is there any browser Add-on that separates cookies based on folder?
Call session_set_cookie_params() to restrict the cookie to a particular folder:
$params = session_get_cookie_params();
session_set_cookie_params($params['lifetime'], '/folder1');
session_start();
Yes. Creating subdomains should solve your problem. I do this all the time.
If you are opening your site as 66.102.0.0 then your session is based on that url which is causing all the problems.
Note: Directories mentioned here are windows wamp specific but as long as you find the appropriate files, this applies to Apache in all OS's.
Step 1. Uncomment this line in your bin/apache/Apache*.*.*/conf/httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
Step 2. Make a virtual host entries in your bin/apache/Apache*.*.*/conf/extra/httpd-vhosts.conf file.
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "D:/wamp/www"
ServerName localhost
ErrorLog "logs/localhost-error.log"
CustomLog "logs/localhost-access.log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "D:/wamp/www/folder1"
ServerName folder1.localhost
ErrorLog "logs/folder1.localhost-error.log"
CustomLog "logs/folder1.localhost-access.log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "D:/wamp/www/folder2"
ServerName folder2.localhost
ErrorLog "logs/folder2.localhost-error.log"
CustomLog "logs/folder2.localhost-access.log" common
</VirtualHost>
Step 3. Restart wampserver / apache.
Step 4. Edit your hosts file. C:\Windows\System32\drivers\etc\hosts
Add in the entries.
127.0.0.1 folder1.localhost # You probably want 66.102.0.0 over here instead of 127.0.0.1
127.0.0.1 folder2.localhost

Accessing virtual host by ip

I have a Virtual Host in my machine with this configuration:
<VirtualHost 127.0.0.1:80>
ServerName codigos
DocumentRoot /home/code/codigos/app/webroot
SetEnv APPLICATION_ENV "development"
<Directory /home/code/codigos/app/webroot >
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Ok, that works nice when I type this in my browser: codigos/some_path and 127.0.0.1/some_path
But now I'm working with PayPal and I need a url for the IPN. I know that I cant' put localhost in the url, so I suppose that it would be something like this: my.dynamic.ip/some_path/ipn.php ... and thats my problem, I don't know how to configure my apache file to achieve this.
Thanks.
Register at http://www.noip.com/ (or any other dynamic dns site)
Set up their software so that you was possible to ping blabla.noip.com host (or whatever host you've got after the registration and setting up process) and see it resolves to your ip (you can check it with my ip in google)
After that put the given hostname blabla.noip.com to the ServerName or (better) ServerAlias directive
After that you'll be able to access to your virtual host from outside (assuming your firewall doesn't reject connections and you have white IP address)

Categories