dongxi2014 2011-07-22 04:30
浏览 60
已采纳

需要帮助在Word中复制.htaccess mod_rewrite重定向功能

We all know that wordpress have simple .htaccess code like below RewriteEngine on RewriteBase /

# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s 

# pass the rest of the request into index.php to handle     
RewriteRule ^(.*)$ /index.php/$1 [L]

But if I redirect all requests to index.php, I think it becomes pretty cumbersome to handle every rewrite in php. Currently I have a logic in mind, like maintain a db table with all valid redirections. But still I don't know how to handle rules likes ([0-9]+).

If anyone has implemented something like this before or has a logic in mind, can you please guide me in this matter

The sole purpose am doing this, is because I want flexibility in adding/deleting categories in the menu of my site. I don't want to go to .htaccess every time and edit it at all places. I want to create more like a CMS where user can add delete categories

  • 写回答

2条回答 默认 最新

  • dpt62283 2011-07-22 04:54
    关注

    My suggestion would be to set up php based routing the basic idea is that you do something like this:

    RewriteEngine On
    
    # skip all files with extensions other than .html
    # this way if you want you can append the .html to dynamic pages
    # which wont really exist as a file
    RewriteCond %{REQUEST_URI} \..+$
    RewriteCond %{REQUEST_URI} !\.html$
    RewriteRule .* - [L]
    
    // redirect everything to your front controller
    RewriteRule ^(.*)$ index.php [QSA,L]
    

    Notice how you just make everythign go to index.php but you dont rewrite any of the variables or anything. This is because you will work out what to based on the pattern of the URL with php. In order to do this youll need to implement a router. Basically a router takes a request and matches it against a pattern and then determines any parameters based on the pattern.

    There are existing libraries out there to do this for you. For example Zend_Controller_Router.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题