duandai7601 2013-05-15 06:20
浏览 17

使用PHP,MySQL和mod_rewrite从数据库中获取页面

I am currently building my own (very small) CMS. I am using PHP/MySQL and mod_rewrite for dynamically fetching the page requests from the database.

So far I had a plain structure like:

domain.com/page

This all works perfectly fine. But now I want to add "folders" or "categories".

The table has the needed fields (simplified):

id, name, pid (parent id)

I also worked out how to build the navigational structure from the database, but what I did not figure out so far is, how to get the requested page from the database by using mod_rewrite and PHP.

  • How do I get the requested URLs like
    domain.com/folder1/page
    or more complex
    domain.com/folder1/folder2/folder3/.../folderN/page
  • How can I differ the document "page" in "folder1" from the document "page" in "folderN"?
  • How do I treat not existing folders, like "folder2" in the example above?

Uhm, honestly I am a little bit helpless at the moment and hope someone here is willing to help me. :-)

Of course I googled and searched in stackoverflow, but I did not find any specific help. Links to tutorials are also welcome.

Thanks in advance & best regards!

  • 写回答

1条回答 默认 最新

  • dtihe8614 2013-05-16 07:59
    关注

    The main problem here is that Apache can't talk to MySQL so it doesn't know which folders do or do not exist.

    The way most CMSs (like wordpress) solve this is to redirect every request that is not for an existing file or directory to an entry script of the CMS.

    Options +FollowSymLinks -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .? index.php [L]
    

    and then in the index.php check $_SERVER['REQUEST_URI'] to see what the request was for and handle accordingly.

    $parts = explode('/', ltrim($_SERVER['REQUEST_URI'], '/'));
    foreach($parts as $part)
    {
        // see what the part is for, and if it does not exist
        // if you find that the request does not exist, send a 404 back to the browser
        // header($_SERVER['HTTP_PROTOCOL'].' 404 Not Found');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号