0

I was wondering how the url rewrite on for example this site works. I mean I don´t think that it is done via. modrewrite in apache or nginx. The thing I´m interested about is the id and user friendly title in one. How does it work that if the server enters only the id or the id with a wrong title, it 301 rewrites it to the right one ?

Is there any how to about it I couldn´t find or could you explain it for me with a small example?

For Example:

-index.php
|--index.php?page=2
-server.php?id=45343
-contact.php



-example.com/
|--example.com/page/2
-example.com/server/45343/some-random-title-to-be-user-friendly(.html)
-example.com/contact(.html)

.

Update:

I would like to know how this works without modrewrite (htaccess..) as I know that e.g. xenforo isn´t using it.

user2693017
  • 1,750
  • 6
  • 24
  • 50

1 Answers1

0

I am not sure if this is the way it's done on StackOverflow (probably is), but I have had a similar code working.

Basically, all the directories you see in the URL (questions/22239304/title) don't really exist, they are virtual. An .htaccess file in the root directory rewrites all access to a PHP file, which then decides what to do with it. The most important part of the URL is the ID - if it has the ID, it also has the database entry and can get the correct title. If it is incorrect in the URL, it can simply redirect, if not, it servers the page.

Here is an example .htaccess that redirects all traffic to index.php (might want to be more specific, e.g. have rules that only match URLs with 'questions' in them):

RewriteEngine On

RewriteRule ^redir.php$ redir.php [L,QSA]
RewriteRule ^(.*)$ redir.php?q=$0 [L,QSA]

Then in the redir.php file you would look at the $_SERVER["QUERY_STRING"] and parse it appropriately.

Aurel Bílý
  • 7,068
  • 1
  • 21
  • 34
  • mhh. I would like to know how this works without modrewrite, I know for sure that xenforo and IPBoard are not using any modrewrite in a htaccess. Maybe stackoverflow was a bad example. :( – user2693017 Mar 07 '14 at 01:17
  • http://xenforo.com/help/friendly-urls/ It seems to me they do use .htaccess. If not, then they just have the same 'friendly URLs' just passed directly to the php file. E.g. `index.php?question/111/blah` – Aurel Bílý Mar 07 '14 at 01:23
  • ok and how does it work that if you enter any different url but with the same id you get redirected to the right one ? `http://www.example.com/threads/its-fun-to-change-this.12345/` -> `http://www.example.com/threads/thread-title-here.12345/` – user2693017 Mar 07 '14 at 02:43
  • As I have written in my answer, the ID number is the most important bit. Usually everything which does 'nice URLs' has them, or something equally unique to the post. So if it has the right ID number in the URL, it compares the rest, which is actually readable, with what it should be based on the title of the post or whatever. If it matches, it servers the page, if not, it does a 301 header redirect and the browser navigates there. – Aurel Bílý Mar 07 '14 at 02:47
  • so the 301 redirect is executed by php ? – user2693017 Mar 07 '14 at 02:52
  • 1
    Yes. `header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.example.com/".$url); ?>` – Aurel Bílý Mar 07 '14 at 02:55
  • ahh thanks, thats what I was looking for. btw. is there a way to make such a apache rewrite automated, by this I mean that you don´t need to specify `redir.php` and so on ? Like everytime you have `?` or `&` it rewrites it on the .phps name ? – user2693017 Mar 07 '14 at 04:34
  • I am not sure what you mean. The .htaccess I posted would redirect pretty much all traffic to the redir.php file to be further redirected by the PHP code. It is very greedy and so having more specific rules helps. Also, please accept the answer, unless you have any more questions. – Aurel Bílý Mar 08 '14 at 03:37
  • ahh now I understand it. Thanks – user2693017 Mar 08 '14 at 05:19