skip to Main Content

I’ve been wondering for quite a while how do you set up php so that you instead of getting content from tables w/ get like www.mysite.com/index.php?id=1 you get them by www.mysite.com/pages/news-1.php

I have no idea how else to make up this question; but do I have to manually create new pages and put them in directories then link the page/ID via db or is there another way with mysql only.

2

Answers


  1. With URL rewriting, you need to route all page requests to your “front controller” / index.php. Then you use a router to send that request to a particular controller method. In that method, you would grab the request’s path (eg. /postname). Your posts database table would have a “slug” field. You then query the database for the “posts” result that contains the slug “postname”.

    Please check out this tutorial to learn how to make a custom framework, it’ll give you the amount of background information that might shed some light on how this all works: http://symfony.com/doc/master/create_framework/index.html

    Login or Signup to reply.
  2. Create a file with name .htaccess in your web root directory. And paste these lines into it (Tested):

    RewriteEngine On    # Turn on the rewriting engine    
    RewriteRule    ^pages/news-([0-9]+)/?$     index.php?id=$1    [NC,L]
    

    URL rewriting can be one of the best and quickest ways to improve the
    usability and search friendliness of your site. It can also be the
    source of near-unending misery and suffering. Definitely worth playing
    carefully with it – lots of testing is recommended. With great power
    comes great responsibility, and all that. Aug 4, 2008 – addedbytes.com

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search