What's the mecanism behind a URL of a file/path that doesn't exist and yet get displayed?

StackOverflow https://stackoverflow.com/questions/23601749

  •  20-07-2023
  •  | 
  •  

Question

I have had a website developed for me in PHP and I'm trying to understand how some of its features work. For instance I have the following URL.

http://www.hilariousworld.com/On2wheelz/index.php/admin/adddeals/adminhtml_adddeals

There's a file called index.php on the server but there's no folder called that way and yet the URL continues on with /admin/adddeals/adminhtml_adddeals which I can't seem to find. My guess is that this is some kind of "php magic trick" but I don't know what.

Could someone explain to me or give me a hint or direct me to some documentation or tell me what should I search for on Google to figure out how could this work?

Was it helpful?

Solution 3

Well if you see Some MVC Frameworks you see some code in a special file .htaccess like that.

RewriteEngine on    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L] 

This rules define the Url rewriting mechanism. So your example url is treated as :

Actual Url :  http://www.hilariousworld.com/On2wheelz/index.php
Query String :   /admin/adddeals/adminhtml_adddeals

As .htaccess already redirected all request to index.php with its query string parts. Now frameworks handle the Query String Part to determine what actual controller/action combination (in case of non-mvc applications requested content action ) need to serve.

OTHER TIPS

The way URLs are handled depend on the actual framework you are using. A simple way to do this is via .htaccess eg.

1  RewriteEngine on
2  RewriteCond %{REQUEST_FILENAME} !-f
3  RewriteCond %{REQUEST_FILENAME} !-d
4  RewriteRule ^(.*)$ index.php?first_param=$1&second_param=$2 [L]

This is what the above code means:

  1. Turn the rewrite engine on
  2. If the file requested in the URL doesn't exist then follow the rewrite rule
  3. If the directory requested in the URL doesn't exist then follow the rewrite rule
  4. The rewrite rule: Anything after the base URL is used as a GET variable which isn't visible to the user, eg. www.example.com/link/4 will be interpreted as www.example.com?first_param=link&second_param=4

Now this is the most simplest way to do this kind of thing, CMSs such as WordPress and Drupal have their own way of handling URLs.

You can see more about .htaccess URL rewrite here: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

You can stick /anythingYouLike after filename.php in the URL. PHP handles it natively.

The full URL is available through $_SERVER.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top