dongqishou7471 2012-11-20 17:24
浏览 47
已采纳

根据变量的数量,使用mod_rewrite重定向到不同的页面

I am currently using the mod_rewrite rule:

RewriteRule ^(.+)/?$ page.php?slug=$1 [L]

This works fine to make /page load page.php?slug=page.

However, I want to URL:

/cat/page

to load

page.php?slug=page

And the URL

/cat

to load the page

cat.php?slug=cat

Is this possible? I could do it on one page.php and use an if to determine which page it should be, however as the code for the category page and the page page is so different, I'd much rather keep them separate.

Thank you,

Sam

Update: More examples, as requested.

/food/pizza loads page.php?slug=pizza

/food loads cat.php?slug=pizza

/animals/pigs loads page.php?slug=pigs

/animals loads cat.php?slug=animals

  • 写回答

1条回答 默认 最新

  • dongzhitao4839 2012-11-20 20:59
    关注
    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !=page.php
    RewriteCond %{REQUEST_URI} !=cat.php
    RewriteRule ^([^/]+)/([^/]+)/?$ cat.php?slug=$2 [L]
    
    RewriteCond %{REQUEST_URI} !=page.php
    RewriteCond %{REQUEST_URI} !=cat.php
    RewriteRule ^([^/]+)/?$ page.php?slug=$1 [L]
    

    I'm not sure about the names of parameters to cat.php and page.php as your examples are not consistent (sometimes it's slug and sometimes cat?) Anyhow it's minor problem.

    Description: the RewriteCond make sure page.php and cat.php will not be rewritten. Then there are regular expressions matching one or two levels of virtual directories passing the request to correct PHP script.

    EDIT: not. it's passing it to really the correct script :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误