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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度