drjyvoi734793 2014-07-01 20:27
浏览 22
已采纳

重写并区分同一级别的两个GET参数

I have a PHP script that outputs different stuff based on GET parameters.

PHP code:

if((isset($_GET['channel'])) && (!isset($_GET['profile']) && !isset($_GET['c']) && !isset($_GET['b']) && !isset($_GET['highlight']) && !isset($_GET['broadcast']))){

  echo 'Output1';

} elseif((isset($_GET['channel']) && isset($_GET['profile'])) && (!isset($_GET['c']) && !isset($_GET['b']) && !isset($_GET['highlight']) && !isset($_GET['broadcast']))) {

  echo 'Output2';

} elseif((isset($_GET['channel']) && isset($_GET['c']) && isset($_GET['highlight'])) && (!isset($_GET['profile']) && !isset($_GET['b']) && !isset($_GET['broadcast']))) {

  echo 'Output3';

} elseif((isset($_GET['channel']) && isset($_GET['b']) && isset($_GET['broadcast'])) && (!isset($_GET['profile']) && !isset($_GET['c']) && !isset($_GET['hightlight']))) {

  echo 'Output4';

}

Current .htaccess:

RewriteEngine on

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ ?channel=$1&b=$2&broadcast=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ ?channel=$1&c=$2&highlight=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ ?channel=$1&profile=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ ?channel=$1 [L,QSA]

How I need it to work:
example.com/channel/c/12345 outputs "Output3"
example.com/channel/b/12345 outputs "Output4"

How it works now:
example.com/channel/c/12345 outputs "Output4"
example.com/channel/b/12345 outputs "Output4"

Are there any clever ways to make this work like I want it to?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • doudie2693 2014-07-01 20:45
    关注

    In the second capture group, you can just match b or c directly if you are looking for those literals, no need to search for any character

    RewriteRule ^([^\/]+)/([b])/([^\/]+)/?$ ?channel=$1&b=$2&broadcast=$3 [L,QSA]
    RewriteRule ^([^\/]+)/([c])/([^\/]+)/?$ ?channel=$1&c=$2&highlight=$3 [L,QSA]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改