mod_rewrite, php and the .htaccess file

前端 未结 2 1812
轮回少年
轮回少年 2020-11-30 12:57

I\'m coding a small CMS to get a better understanding of how they work and to learn some new things about PHP. I have however come across a problem.

I want to use mo

2条回答
  •  無奈伤痛
    2020-11-30 13:48

    Your PHP should for very obvious reasons not be able to modify .htaccess. Even if you get that to work, I'm not sure if it is wise.

    How about using a more abstract setup in regard to mod_rewrite rules? Define your general URL pattern, as you would like to use it. For example:

    /object/action/id
    

    Then write a set of rules that reroute HTTP requests to a PHP page, which in turn makes the decision what page is to run (say, by including the relevant PHP script).

    RewriteRule ^/(\w+)/?(\w+)?/?(\d+)?$ /index.php?object=$1&action=$2&id=$3 [nocase]
    

    This way you would not have to update .htaccess very often an still be flexible.

提交回复
热议问题