-1

For example, how does facebook make their url for their settings page "https://www.facebook.com/settings" and how does Reddit make the subreddit urls like this: http://www.reddit.com/r/askscience/

I am working on a site and I'd like to incorporate this for simple and user friendly navigation. What is the simplest way to do this in PHP?

shakked
  • 783
  • 8
  • 24
  • Doesn't reddit run on webpy? – Dylan Madisetti Jan 05 '14 at 05:19
  • EDIT: bit of research shows Reddit runs on Pylons http://highscalability.com/blog/2013/8/26/reddit-lessons-learned-from-mistakes-made-scaling-to-1-billi.html regardless, m59 is on the right track – Dylan Madisetti Jan 05 '14 at 05:26
  • possible duplicate of [How to code a URL shortener?](http://stackoverflow.com/questions/742013/how-to-code-a-url-shortener) – Igy Jan 06 '14 at 21:29

3 Answers3

1

There are plenty of frameworks that can simplify this and add a lot of functionality to the process, but I'll show you a basic approach:

First, you can use htaccess to route all requests to one file:

//.htaccess - route all requests through index.php

//don't route the request if the url is for a file or directory
RewriteCond %{REQUEST_URI} !\.(png|jpe?g|gif|css|js|html)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

//all requests are sent to the file "index.php"
RewriteRule ^.*$ index.php [L]

Then, you can parse the request url with php and respond to it accordingly:

$pieces = preg_split('-/-', $_SERVER['REQUEST_URI'], NULL, PREG_SPLIT_NO_EMPTY);
$page = $pieces[0];
include "pages/{$page}.php";

Obviously, this is only a VERY basic example. The results of this would be:

//site.com/some-place
include "pages/some-place.php
m59
  • 43,214
  • 14
  • 119
  • 136
  • i'm sorry i'm have trouble understanding this. the first one makes sense, but could you explain the second one piece by piece – shakked Jan 05 '14 at 05:15
  • @user2554352 trouble understanding what about it? – m59 Jan 05 '14 at 05:15
  • so the first part, if I copy and pasted that into my index.php file, would redirect all requests to my index.php file – shakked Jan 05 '14 at 05:17
  • @user2554352 no, that's htaccess. You need to make a file called `.htaccess` in the root directory with `index.php` and add that code to it. You'll need to put `RewriteEngine on` at the beginning of the `htaccess` file, also. `htaccess` is an Apache server module, not php. – m59 Jan 05 '14 at 05:21
0

You need to use apache's mod_rewrite.

You can set up simple rules in a .htaccess rule that maps each of the segments in the url to specific GET variables in php.

Here's an example:

RewriteEngine on

RewriteBase /
RewriteRule ^([\ A-Za-z0-9-_,]+)/?$ /index\.php?mode=$1 [NC,L]
RewriteRule ^([\ A-Za-z0-9-_,]+)/([\ A-Za-z0-9-_,]+)/?$ /index\.php?mode=$1&slug=$2 [NC,L]

this will make the url /p/home map to index.php?mode=p&slug=home

  • 1
    It isn't ideal to use htaccess to route many things. It should be used only to route all requests to one file and then have php do the routing. – m59 Jan 05 '14 at 05:16
  • Why isn't it ideal? It's lightweight, and simple to write, understand and use which is what the OP needs as they are a beginner by the sound of it. –  Jan 05 '14 at 05:17
  • 1
    It isn't lightweight. `.htaccess` is known for poor performance. This is also a messier/less flexible/less powerful approach. – m59 Jan 05 '14 at 05:19
  • @m59 when I said lightweight, I meant on the end of the newbie programmer, and also in terms of simply setting up SEF urls without modifying the rest of the program to operate around a router, it's a lighter weight solution, and in terms of the performance hits, that's largely a legacy issue, and as long as you're not running .htaccess files in hundreds of directories, the performance impact is negligible. –  Jan 05 '14 at 05:24
  • 1
    Well, I have a very basic routing example with php... you have your example. I'll leave the decision to the OP. – m59 Jan 05 '14 at 05:27
0

You're asking about some things that are a bit beyond what you would normally do with just plain old 'vanilla' PHP.

If you are using a framework, such as Code Igniter, you would set up a controller to handle that URL, and gather whatever data you need and load your view based on that.

You would more likely use Apache url rewrites to achieve this (if your webserver is Apache. If not, there are similar roles for Node.js or Google App Engine, and others).

Chad Chabot
  • 313
  • 3
  • 15