-1

I want to set something to the URL when the page is loading ... How can I do that?


When you open this URL:

http://stackoverflow.com/questions/38839016/should-i-store-the-result-of-an-function

automatically it will be replaced with this:

http://stackoverflow.com/questions/38839016/should-i-store-the-result-of-an-function-into-an-array

How does SO do that?


Note: I've disabled JavaScript on the browser, still that replacement happens. So it is done by PHP.

Martin AJ
  • 6,261
  • 8
  • 53
  • 111
  • No, it's not done by PHP, Stack Overflow uses Microsoft software on their servers, not PHP. Even when using PHP, you'd probably have the server, i.e. Apache most of the time, do the redirect. – adeneo Aug 08 '16 at 22:48
  • Read more about `.htaccess` and Permalinks. – Praveen Kumar Purushothaman Aug 08 '16 at 22:49
  • 1
    @PraveenKumar Ah .. Is replacing spaces with `-` is also done by `.htaccess`? – Martin AJ Aug 08 '16 at 22:51
  • 1
    Thats a technique called **url rewriting in php** you can do this and any other way you want the url to be.. just read about how to do url rewritting resources – StackB00m Aug 08 '16 at 22:52
  • Just to verify, I don't think this site is running PHP at all. http://builtwith.com/stackoverflow.com – OneCricketeer Aug 08 '16 at 22:56
  • 2
    To [verify more correctly](http://nickcraver.com/blog/2016/02/17/stack-overflow-the-architecture-2016-edition/), this site is running 4 Microsoft SQL Servers, 11 IIS Web Servers, 2 Redis Servers, 3 Tag Engine servers, 3 Elasticsearch servers, 4 HAProxy Load Balancers, 2 Networks (each a Nexus 5596 Core + 2232TM Fabric Extenders, upgraded to 10Gbps everywhere). 2 Fortinet 800C Firewalls (replaced Cisco 5525-X ASAs) 2 Cisco ASR-1001 Routers (replaced Cisco 3945 Routers) 2 Cisco ASR-1001-x Routers (new!) – adeneo Aug 08 '16 at 23:00
  • @MartinAJ Yes buddy. The first answer explains everything... :) – Praveen Kumar Purushothaman Aug 08 '16 at 23:08

2 Answers2

3

It's a simple HTTP redirect. Each question has a slug. When the post slug doesn't match with the slug segment of the url, the HTTP controller redirects the page to the correct route.

Ram
  • 143,282
  • 16
  • 168
  • 197
  • Well I can do that redirect by PHP ..! Why not? BTW I don't know how HTTP access the title and how it can compare it with the real title... – Martin AJ Aug 08 '16 at 23:03
  • That is correct (+1). The real identifier is the number `38839016`, the last part of the URL is "meaningless", in that *anything* will lead you to the correct question, with the correct "slug". The last part of the URL is mostly used for SEO and such things, and does not identify the question the URL links to – adeneo Aug 08 '16 at 23:04
  • http://stackoverflow.com/questions/47427/why-do-some-websites-add-slugs-to-the-end-of-urls – adeneo Aug 08 '16 at 23:06
  • @adeneo I see. Just one thing: that id `38839016` and that title are stored in the database.. So I **only** can do a *compare* and *redirect (it if is needed)* by PHP, not anything else like `.htaccess`. Right? – Martin AJ Aug 08 '16 at 23:06
  • 1
    You can do whatever you want, it's your site, and your server. Of course you can create a "controller" in PHP that checks the URL for it's parts, and finds the right content based on one part etc. the URL doesn't have to point to an actual file, what is served, is up to you. – adeneo Aug 08 '16 at 23:08
  • Got it .. Thank you @adeneo And Vohuman .. upvote – Martin AJ Aug 08 '16 at 23:09
1

You can define many rules about rewriting the url address in apache .htaccess file. There magic happens.

You can look in more detail how to define rewrite rules in .htaccess here.

von Oak
  • 823
  • 5
  • 14