0

How do CMS's such as MediaWiki, Drupal, Wordpress etc. display the correct pages when a URL is for a directory/file which doesn't exist.

To clarify, if I go to the url https://en.wikipedia.org/wiki/Example, there is no directory on wikipedia's server /wiki/Example, instead MediaWiki creates the page from templates and information in databases etc. I'm asking how the CMS "Hijacks" the request for that directory/file in order to send it's own page back rather than a 404.

I'm asking with regards to php as that's what I'm using and what most CMS's seem to be primarily based on.

SlipperyPete
  • 9
  • 1
  • 4
  • this is called url rewriting. It can be implemented through the webserver configuration or php directly. – Calimero Sep 27 '17 at 08:14
  • Possible duplicate of [How can I create an error 404 in PHP?](https://stackoverflow.com/questions/1381123/how-can-i-create-an-error-404-in-php) – GGO Sep 27 '17 at 08:16
  • @Calimero A quick search for that term seems to give me most of the information I'm looking for, Basically change server settings to allow it and tell where to look, and have the php file or other figure it out from there. – SlipperyPete Sep 27 '17 at 09:01
  • _Accidently hit enter and took to long editing_ Pretty Much what I expected, hoped it didn't require server settings as that's platform dependent and not necessarily always available, but I can figure it out. If you wanted to write that as an answer I'd be happy to accept it. – SlipperyPete Sep 27 '17 at 09:07
  • Drupal have a fast_404 feature to make it esay : https://www.drupal.org/project/fast_404 – Fky Sep 27 '17 at 09:26
  • @Fky, Sorry How is that related? – SlipperyPete Sep 27 '17 at 10:51

2 Answers2

0

The basic concept is very simple:

  1. Use a custom 404 instead of the default
  2. Dynamically generate content based on the entered url
user2426320
  • 81
  • 1
  • 11
0

Web server (i.e. Apache, Nginx...) has possibility to catch requested URL and to convert them to something else, some other URL. Most common way is by using .htaccess file placed in site's root directory. In some systems files starting with dot (".") are hidden by default so you must enable somewhere something in order to see them at all.

It contains rules (among other things) how to recognize some routes or set of routes and what to do with them them.

I.e. you have "virtual" path like:

/event/32

You'll create a rule to catch every path that starts with "/event/" and also catch part after that ("32") and instead of opening un-existing directory it will call some script like:

 /event.php?event_id=32

So that captured parameter has been sent as get parameter "event_id" to event.php script.

For this search/replace and capturing path part regular expressions are used.

You have many tutorials online how to do that, just google for them:

https://www.addedbytes.com/blog/url-rewriting-for-beginners

https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules

https://help.dreamhost.com/hc/en-us/articles/215747748-How-can-I-redirect-and-rewrite-my-URLs-with-an-htaccess-file-

....

Main server configuration is placed in some other server directory, most likely accessible only for server admin. But this .htaccess file a a way to allow "common users" to do some configuration changes, which will apply only for directory where that file is placed and it's children.

But you can also do a lot of other thins with .htaccess file, i.e. send some header parameters, allow/forbid access to some files/directories, set some simple login mechanism (password protection) and much more...

MilanG
  • 6,994
  • 2
  • 35
  • 64
  • Thanks for your answer, I was just in the middle of writing my own but I would prefer not to answer my own question and yours is much more complete for future people reading this. – SlipperyPete Sep 27 '17 at 13:01