Apache : URL Redirection when using Virtual Hosts - php

I have 2 domain with 2 differents registar, the problem is I would like to to point them in my Dedicated server with the same IP, so I have used Virtual Hosts with this configuration :
My httpd.conf looks like this:
<VirtualHost *:80>
DocumentRoot /var/www/html/
ServerAdmin webmaster#xxxxxxxx.com
ServerName www.xxxxxxxx.com
DirectoryIndex index.php
ErrorDocument 404 "http://xxxxxxxx.com/error"
</VirtualHost>
# Virtual host Virtual Host 2
<VirtualHost *:80>
DocumentRoot /home/www/html/yyyyyyyy/
ServerAdmin webmaster#yyyyyyyy.com
ServerName www.yyyyyyyy.com
ServerAlias yyyyyyyy.com
RewriteEngine on
ErrorDocument 404 "http://yyyyyyyy.com"
</VirtualHost>
And I changed the Ip of the two domain to the server IP, this has work fine for the first website but for the second one , when entring the URL it changes automaticaly to this url :
www.xxxxxxxx.com/yyyyyyyy/
In other terms, the url is changed.
What I need to do to not permit the rewrite of the URL of the second website.
Please Help...

you need to do this in Cpanel not in httpd.conf
on the receiving(hosting) server select the addon domain and enter the domain you wish to accept and the directory it should point to
then change the IP address in your DNS A records of your redirect domains to point to your hosting server (make sure to copy the old IP address just in case you need to roll back, I recommend a screenshot)

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/

How to access a site on Codero without a domain name and without appending the username to the end

Yesterday i have created a new site on my Codero Dedicated Server. and i have not assigned any domain name yet to the ip address.
Currently i am able to access my site with http://MySiteIpAddress/~username but i want to access it with only http://MySiteIpAddress/. Is there any way to do it. I don't know if that's possible by htaccess rules, so any hints are appreciated.
You want to setup a virtual host and point it to the folder you want.
See this other post for more details.
You need to do several steps in order to make this work.
1.) Update the hosts file. On Windows XP, you can find it under c:\WINDOWS\system32\drivers\etc\. You should already see the first
line from below, it takes care of your mentioned other project. - add
the additional ones to make any requests to the mentioned virtual
hosts routed back to your own machine.
127.0.0.1 localhost
127.0.0.1 foo-bar.com
127.0.0.1 abcdef.com
127.0.0.1 qwerty.com
2.) Update the vhosts file in Apache configuration. Under your XAMPP folder, add the following to
apache\conf\extra\httpd-vhosts.conf and if needed change the ports
(i.e. if you use 8080 instead of port 80).
<VirtualHost *:80>
DocumentRoot C:/xampplite/htdocs/foo-bar/
ServerName www.foo-bar.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot C:/xampplite/htdocs/abcdef/
ServerName www.abcdef.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot C:/xampplite/htdocs/qwerty/web/
ServerName www.qwerty.com
</VirtualHost>
3.) Do a quick configuration check. Open {XAMPP-folder}\apache\conf\httpd.conf your file and make sure that
the following part is not commented out by a preceeding # character:
Include conf/extra/httpd-vhosts.conf
4.) Restart XAMPP.
... and you should be all setup now. Your other project should
be accessible at the URI you mentioned if you just put it under
C:/xampplite/htdocs/my-project/.

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.

Apache Virtual Hosts Not Working As Expected

My Apache "httpd-vhosts.conf" looks like this::
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/"
ServerName localhost
ServerAlias *.localhost
</VirtualHost>
<VirtualHost laravel.dev:80>
DocumentRoot "c:/wamp/www/laravel/public"
ServerName laravel.dev
ServerAlias *.laravel.dev
</VirtualHost>
<VirtualHost learninglaravel.dev:80>
DocumentRoot "c:/wamp/www/learningLaravel/public"
ServerName learningLaravel.dev
ServerAlias *.learningLaravel.dev
</VirtualHost>
and my "...system32/drivers/etc/hosts" also looks like this::
127.0.0.1 localhost
127.0.0.1 localhost
// I added the following entries. The first two entries above was already there
127.0.0.1 laravel.dev
127.0.0.1 learninglaravel.dev
When i enter "learningLaravel.dev" and "laravel.dev" into the browser, they work fine as expected. But i have other folders in my "www" folder that i use them to learn PHP and i want to be able to access the files in those folders directly from the browser like say "localhost/test/me.php". But anytime i enter such address the browser goes to the second entry in the vhosts-conf file [which prints a laravel error meaning that it can't find the file]. It seems that the first entry in the vhosts-conf file is not working and Apache bypasses it to the second entry. The first entry is suppose to be the catch all entry. I tried to swap the second and third entries to see how it will behave but it always direct the browser to the second entry instead of the catch all (first entry) that is suppose to handle addresses likes "localhost/test/me.php"
Anytime i enter only "localhost" into the browser, it goes straight to the second entry instead of say printing the contents of the "www" folder.
How do i solve this problem? thanks.
It seems the problem comes from the way you use the VirtualHost directive.
Using a fully qualified domain name for the IP address of the virtual host is not recommended. It is misleading how it works. Name based virtual hosts determine the host through the ServerName directive, and not through the FQDN in the VirtualHost directive (<VirtualHost FQDN:80>). In fact this is seen as <VirtualHost 127.0.0.1:80>
What happens is your case is documented in the VirtualHost doc, last 2 paragraphs (just before "Security"), quoted:
When a request is received, the server first maps it to the best
matching based on the local IP address and port
combination only. Non-wildcards have a higher precedence. If no match
based on IP and port occurs at all, the "main" server configuration is
used.
If multiple virtual hosts contain the best matching IP address and
port, the server selects from these virtual hosts the best match based
on the requested hostname. If no matching name-based virtual host is
found, then the first listed virtual host that matched the IP address
will be used. As a consequence, the first listed virtual host for a
given IP address and port combination is the default virtual host for
that IP and port combination.
So when you ask for localhost/somedir, the server will try to find from the non-wildcards VHosts declarations, but do not find any with corresponding host name (ServerName), and so it chooses as "default" the first VHost with IP:Port, and not the first with *:Port.
To solve your problem, try to use <VirtualHost *:80> in all three vhost declarations :
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/"
ServerName localhost
ServerAlias *.localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/laravel/public"
ServerName laravel.dev
ServerAlias *.laravel.dev
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/learningLaravel/public"
ServerName learningLaravel.dev
ServerAlias *.learningLaravel.dev
</VirtualHost>
And reload / restart Apache.
(My only doubt about this is why Nasreddine could make a working test case with your setup.)

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