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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化