CrimsonKnights
CrimsonKnights

Reputation: 77

Redirect Traffic to Different pages bases on the address used

I Have a webserver running on my raspberry pi and I am using it for multiple projects. I can easily enough access the different pages with something of the from ip-addrss\project-name.php. I was looking to eliminate the ip address and found I could set up the domain names on my router. so http:/projector or projector.local redirects to the raspberry pi. The problem is it always goes to the default page. I can setup http:/ProjectA and http:/ProjectB but they both go to index.php. is there a way in php to redirect based on the url used to get there. so index.php would redirect to projectA.php or projectB.php depending on which url was used? I looked through $_SERVER and $_POST but they didn't seem to have the right information. Some research lead me to believe apache could do this but I have experience configuring apache.

Upvotes: 0

Views: 40

Answers (1)

Alex
Alex

Reputation: 14628

You COULD do it in PHP, by checking $_SERVER['HTTP_HOST'], but that value can be manipulated by who is making the request. So I can access http://ProjectA while specifying the headers host: ProjectB or similar, and you will think that it's ProjectB.
In fact, if you look at the HTTP request, HTTP_HOST is the only way one would determine the domain name. So it doesn't matter if you do it in PHP or Apache.

In Apache, you could do it by enabling vhosts mod for apache. If you're running linux, the command line might be something like this a2enmod vhosts_alias. This will allow you to configure different hosts, determined by the host HTTP header, and IP. Each virtual host points to an individual directory. You can have 2 hosts pointing at the same directory, but you'd have to modify the directory properties, something like this:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName ProjectA
    ServerAlias www.ProjectA #you can skip this line if it doesn't apply
    DocumentRoot /var/www/foo
    <Directory "/var/www/foo">
        DirectoryIndex ProjectA.php
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I didn't try to configure the same directory differently for 2 different hosts. My instincts say that it should work, but it may not.

Here's a guide on how to configure virtual hosts on Ubuntu. https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts

I have no idea how different it is on Raspberry Pi. But the apache config files should have exactly the same syntax and rules. Only paths and commands might differ.

Upvotes: 1

Related Questions