Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make nginx virtual directories accessible in php?

Tags:

php

nginx

Let's say I have a web server (nginx) server.com where I have only one php file index.php (there is no directory structure). I want to be able to access anything after server.com. It will be an url structure. For example server.com/google.com, server.com/yahoo.com.au etc...

An example would be http://whois.domaintools.com/google.com (They don't have a directory that's called /google.com, right?)

Q1: How can I access whatever is after 'server.com' from index.php

Q2: Can I get the protocol from such URL? For example server.com/http://www.google.com or server.com/https://www.google.com

PS I'm not certain if the term virtual directory is used here correctly. I just want to do what I saw somewhere else.

like image 485
Radek Avatar asked Jul 17 '12 11:07

Radek


1 Answers

location / {
    rewrite ^/(.*)$ /index.php?q=$1
}

location = /index.php {
    #Do your normal php passing stuff here now
}

Is that what you were looking for?

As an answer to your second question, you can parse the protocol in php. Nginx doesn't need to do that. To parse the url, you can use the parse_url function

like image 115
matzahboy Avatar answered Oct 25 '22 08:10

matzahboy