【问题标题】:url variable is set after rewrite ruleurl 变量在重写规则后设置
【发布时间】:2021-09-09 17:13:05
【问题描述】:
RewriteCond %{THE_REQUEST} \s/+blog/index\.php\?c=([^\s&]+) [NC]
RewriteRule ^ blog/%1? [R=301,L]
RewriteRule ^blog/([\w-]+)?$ blog/index.php?c=$1 [L,QSA]

以上工作正常
现在,如果没有 c 变量,我需要一个规则 - 只是 index.php

RewriteCond %{THE_REQUEST} \s/+blog/index\.php [NC]
RewriteRule ^blog/ [L,R=301,NE]  

php

if(isset($_GET['c'])){$cat = $_GET['c'];}
var_dump($cat); // empty string

我不希望在第二种情况下设置 c 变量
是什么原因?

【问题讨论】:

    标签: .htaccess redirect mod-rewrite url-rewriting query-string


    【解决方案1】:

    使用您显示的示例/尝试,请尝试遵循 htaccess 规则。请确保在测试您的 URL 之前清除您的浏览器缓存。

    RewriteEngine ON
    ##Rules for uris which have only index.php in it.
    RewriteCond %{THE_REQUEST} \s/(blog)/index\.php\s [NC]
    RewriteRule ^ %1/index.php? [L,R=301,NE,NC] 
    
    ##Rules for uris which have c variable added in it.
    RewriteCond %{THE_REQUEST} \s/(blog)/index\.php\?c=([^\s&]+)\s [NC]
    RewriteRule ^  %1/%2? [R=301,L]
    
    ##Rules for non-existent pages.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(blog)/([\w-]+)/?$  $1/index.php?c=$2 [NC,L,QSA]
    

    OR 以上规则集对没有查询字符串的 URL 进行重定向到 index.php,如果您不想执行重定向,请尝试以下规则,使确保一次只使用一个以上或以下规则。

    RewriteEngine ON
    ##Rules for uris which have only index.php in it.
    RewriteCond %{THE_REQUEST} \s/(blog)/index\.php\s [NC]
    RewriteRule ^ %1/index.php? [L,NC] 
    
    ##Rules for uris which have c variable added in it.    
    RewriteCond %{THE_REQUEST} \s/(blog)/index\.php\?c=([^\s&]+)\s [NC]
    RewriteRule ^ %1/%2? [R=301,L]
    
    ##Rules for non-existent pages.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d    
    RewriteRule ^(blog)/([\w-]+)/?$ $1/index.php?c=$2 [NC,L,QSA]
    

    【讨论】:

    • 缓存已清除。在这两种情况下 - 都不起作用。第一种情况 - 如果我写 index.php in url - 没有重定向。第二种情况 - url 中没有 index.php - c 变量仍然存在
    • @qadenza,当然,我现在已经编辑了我的规则。请检查一次,然后让我知道情况如何,干杯。请确保在测试 URL 之前清除浏览器缓存。
    • 第二种情况 - 结果相同。第一种情况 - 似乎重定向有效,但仍有 c 变量。如果有帮助 - 这是页面 - https://abuena.net/blog/index.php 非常感谢您的努力
    • @qadenza,对不起,我在这里尽了最大的努力,我现在已经编辑了我的规则,请你测试一下,让我知道它是怎么回事。
    • 第一种情况 - 重定向有效,这就足够了。你是一个真正的英雄。仅供参考 - 第二种情况 - url 仍然是 index.php 但没关系 - 解决了
    猜你喜欢
    • 2017-01-02
    • 2012-11-05
    • 2013-06-08
    • 2016-10-17
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多