As many of you know when someone creates a StackOverflow question a new page is generated and it is visible in the various search engines as well. I was wondering how PHP code created these pages. It turns out that most websites like this have one page, something like profile.php and it loads the data from a MySQL database. My question is as follows: If there is only one physical file how can the URL change and how does google list all of the pages/profiles/question from sites like StackOverflow or Facebook if there is only one actual page?
-
google don't know it is one page or not – Madhawa Priyashantha Sep 12 '16 at 19:23
-
Do you mean like a template? Where the top menu is always the same but the content is different? Or am I way off? – Jethro Hazelhurst Sep 12 '16 at 19:24
-
It is vary because of different id or value in query string – Zeshan Sep 12 '16 at 19:26
-
Search indexers index a URI. In this specific page the URI contains 3 GET parameters, modified by *htaccess*. From those 3 GET parameters the site will render differently. – Xorifelse Sep 12 '16 at 19:26
-
A rewrite is used. E.g. `http://stackoverflow.com/questions/39457314/` the `39457314` is being passed the DB id, then that question's content is loaded. The rewrite is probably something like `Rewrite Rule ^questions/(\d+)/ question.php?id=$1` then `$_GET['id']` is used on the PHP page. I don't know the complete URL structures here so gonna vary. – chris85 Sep 12 '16 at 19:27
-
1I think it kinda works the same as with Symbolic Links in Unix systems. The paths are created, but don't actually exist. Instead they all point to a single file that queries the required data from a database depending on the URI (path) that was called. Search engines simply index the URI and the data on that page. The more popular the website, the faster these indexes take place. – icecub Sep 12 '16 at 19:28
-
Like if you write a question on this site it does not create a new html page it does somehting like ../example.php?id=20 or something like that. How does google know that this is a seperate page if it is the same php file with new data in it – Jack Sep 12 '16 at 19:28
-
Because the URLs are different `http://stackoverflow.com/questions/39457314/` != `http://stackoverflow.com/questions/60174/`. – chris85 Sep 12 '16 at 19:30
-
I need to understand your question as `how does search engines like google know about the different URL on a website, although it is one file only and the content is generated and come from database ` `posts.php?name=question-js`,`posts.php?name=question-loop` even though this contents turns to be URL like `http://domain.com/question-js` and `http://domain.com/question-loop` – Abdelrahman M. Allam Sep 12 '16 at 19:38
-
Or you ask abut how to make `post.php?name=foo` to `posts/foo` – Abdelrahman M. Allam Sep 12 '16 at 19:49
5 Answers
Look at the URL for the page this very question is on:
http://stackoverflow.com/questions/39457314/how-does-php-create-new-html-pages
There isn't a physical file on the server's harddrive for it. There's just one master script called "show a question page". What will happen, in general, for a dynamically generated page:
- user clicks or otherwise surfs to a dynamic page
- Webserver accepts request, and triggers a rewrite on the url
url gets changed from a "friendly" URL to an "ugly" internal one. While Stackoverflow isn't written in PHP, if it was, you'd end up with something like this internally:
http://stackoverflow.com/showquestion.php?id=39457314
Webserver executes the
showquestion.php
script- Code in the script does any necessary work to build the response page: get the question text, get any answers, etc... format into html, and send HTML to user.

- 356,200
- 43
- 426
- 500
As comments have implied, this is handled by URL rewrites in the HTTP server.
It may help to know that an URL corresponds to a physical PHP file of the same pathname by default, but this can be overridden. You can create Apache rewrite rules so that an URL request is handled by any PHP script.
An example is shown in https://framework.zend.com/manual/1.12/en/project-structure.rewrite.html:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
So all URLs are really handled by index.php
, regardless of what the URL says. In that script, the code can read the original request, and then code can decide what the components of that request mean to the application.

- 538,548
- 86
- 673
- 828
You tell search engines 'Google, Bing, ..etc' for example to see your website by giving them sitemap.xml
listing your pages, allow their web-bots (spider) access too the site and crawl the data and links, you may have one page like posts.php
and it is different with parameters posts.php?id=123
or posts.php?name=mypost
Edited:
If you want use one file posts.php
and make site http://domain.com/posts/posttitle
you need to apply Front Controller Pattern site point link and this implies setting you server Apache/Nginx to redirect the requests to one file (URL rewrite) and this file contain the logic for handle the request or use MVC framework

- 892
- 10
- 16
Apache (and therefore PHP) uses Mod_Rewrite to rewrite the URLs to more readable directory-like names. For example, you can use it to turn
www.example.com/index.php?area=blog&which=entry
into
www.example.com/blog/entry
The HTTP headers do not have to match the actual filesystem, so Apache 'spoofs' them into whatever it wants.
It's probably possible to do without mod_rewrite, but I don't know enough to say

- 2,758
- 12
- 23
-
ok i am somewhat familiar with that but how do i get the PHP page to read the ?id=123 part do I enclose everything in a function or what do I do? – Jack Sep 12 '16 at 19:40
The answer is routing.
Take an example url... httml://www.mysite.com/users/profile/jimmy
If you are using a routing system (most frameworks have one) then whatever comes after the .com/ (called the 'path') is treated like a map. The routing system uses this map to find the data.
Many routing systems break this 'path' up into segments. The example above would be broken into the following segments.
- segment 1: users
- segment 2: profile
- segment 3: jimmy
For most frameworks the routing system would interpret the map like this...
controller/action/param-1/param-2/param-3
'Controller' refers to a 'class' in object oriented PHP
'Action' is another word for a 'function', and is also called a 'method'
finally the parameters refer to the data we want to pass around, perhaps it is a name, usually it is an id.
The router will take the example 'path' above and go searching like this...
Takes segment one. Looks for a 'class' called users. Finds class called users. Looks for function called profile inside class called users. Finds profile function. Passes the data into the function. Executes function with the passed data.
Here is a basic code structure that the router would search
<?php
class Users // the controller
{
function profile($name) // the action
{
'Profile for ' . $name; // the passed data 'jimmy'
}
}

- 3,230
- 7
- 38
- 80