开发者

PHP: Router Framework, Actually Going to the "pretty" URL

开发者 https://www.devze.com 2023-02-09 17:01 出处:网络
I have implemented my router framework where I basically use a lookup table to tell what the url will be.

I have implemented my router framework where I basically use a lookup table to tell what the url will be.

开发者_Python百科

My original urls which had some ugly stuff like domain.com?id=26 for example, will now have: domain.com/words-that-look-nice

But my question is: If all the pages will be looked up and redirected to, the actual HTML markup will be in 1 file called my_page.php - correct? The only difference will be that my_page.php will appear differently to the user depending on the item being looked up, correct?

If so, then how do I do this "masking" of the url ??

I mean, once I know what the url will be, how do I make the page my_page.php serve all the data for that particular page that is looked up?

Thanks and sorry if question is convoluted :)


What you need to do first is work out if you can use "rewrites". If you are running apache with "mod_rewrite" then you will be able to use it.

  1. Create a .htaccess file in the root directory... domain.com/. Put the following in there:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?r=$1 [L,QSA] 
  1. Now what will happen is that all requests in the form of "domain.com/this-is-my-page" are rewritten to the form "domain.com/index.php?r=this-is-my-page"

  2. Setup your router to lookup the value of r in the database. Your table would look like this: ID | URL 55 | this-is-my-page 56 | this-is-my-next-page

  3. so run something like "SELECT ID FROM router WHERE URL=LOWER('{$r}')". ofcourse don't forget to check for sql injection attacks.

  4. You now have you ID and you can populate the page as you do at the moment.

Hope that helps :)

0

精彩评论

暂无评论...
验证码 换一张
取 消