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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?