duanleixun2439 2015-07-06 19:13
浏览 27
已采纳

冗余的Apache RewriteRules,有没有办法缩短它们?

I feel my Rewriterules are a bit redundant. Is there a way to shorten the following htaccess code,into one line please? I'm thinking it would use an * somewhere, but I just can't figure it out xD

RewriteRule ^([^/\.]+)?$ index.php?year=$1 [L]

RewriteRule ^([^/\.]+)/?$ index.php?year=$1 [L]

RewriteRule ^([^/\.]+)/([^/\.]+)?$ index.php?year=$1&month=$2[L]

RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?year=$1&month=$2[L]

RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?year=$1&month=$2&day=$3[L]

RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?year=$1&month=$2&day=$3[L]

RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?year=$1&month=$2&day=$3&post=$4[L]

RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?year=$1&month=$2&day=$3&post=$4[L]

RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?year=$1&month=$2&day=$3&post=$4&action=$5 [L]

RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?year=$1&month=$2&day=$3&post=$4&action=$5 [L]

Thank you!

Currently, a php script checks if each GET parameter is set, and depending on which parameter is available, it does a different task. My PHP code works perfectly, no problem, but I am only wondering if there is a way to shorten the apache rules above. I also realize that this way of parsing GET parameters may create many many loopholes, so I appreciate any advice or feedback! :) Thanks a lot ^_^

  • 写回答

1条回答 默认 最新

  • dpd7195 2015-07-06 19:23
    关注

    You are wanting everything to goto index.php?

    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    
    RewriteRule . index.php$1 [L,NC]
    </IfModule>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部