【问题标题】:RewriteRule doesn't work重写规则不起作用
【发布时间】:2013-07-06 07:26:00
【问题描述】:

我要转换这个网址:

http://ts.thebenamorgroup.com/g2g/category.php?cate=mens

到:

http://ts.thebenamorgroup.com/g2g/category/cate/mens/   

我有这个规则,但它不起作用:

Options +FollowSymLinks
RewriteEngine on
RewriteRule category/cate/(.*)/ category.php?cate=$1
RewriteRule category/cate/(.*) category.php?cate=$1

【问题讨论】:

  • 你确定你的配置正在被拾取...如果这是 htaccess 尝试扔一些垃圾文本看看 apache 是否甚至尝试解析它。
  • 是的,我的 .htaccess 正在工作,我尝试了你的建议
  • 你想用哪种方式转换它?您的解释从 ?cate= 到 /cate/ 而您的代码则相反...
  • @SamHuckaby 到这个表格 g2g/category/cate/(任何值)/我不知道什么是失败的
  • 您的 .htaccess 文件在哪里?是在g2g文件夹吗?

标签: apache .htaccess url mod-rewrite


【解决方案1】:

你的重写是相反的方向。即它重写

http://ts.thebenamorgroup.com/g2g/category/cate/mens/ 

到:

http://ts.thebenamorgroup.com/g2g/category.php?cate=mens

如果你想要重定向

http://ts.thebenamorgroup.com/g2g/category.php?cate=mens

到:

http://ts.thebenamorgroup.com/g2g/category/cate/mens/   

然后你可以使用:-

RewriteEngine on

RewriteBase /

RewriteCond %{QUERY_STRING} cate=(\d+) [NC]
RewriteRule ^g2g/category\.php$ /g2g/category/cate/%1/ [NC,R]

【讨论】:

    【解决方案2】:

    试试:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /g2g/
    
    RewriteRule ^category/cate/(.+?)/?$ category.php?cate=$1 [L]
    
    RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /g2g/category\.php\?cate=([^&\ ]+)&?([^\ ]*)
    RewriteRule ^ /g2g/category/cate/%2?%3 [L,R=301]
    

    【讨论】:

    • 我该如何使用这个规则和多个参数并且这些参数是可选的?
    • @EmilioGort 只需在 $ 之前添加另一个 (.+?) 并使用 $2 反向引用它
    • 谢谢,我正在写这个 RewriteRule ^category/(.+?)/?(.+?)$ category.php?cate=$1&sub=$2 [L] 但无法获得第二个参数
    • @EmilioGort 你不希望/ 在那里是可选的,所以删除?
    猜你喜欢
    • 1970-01-01
    相关资源
    最近更新 更多