【问题标题】:$_GET & .htaccess | PHP ignorer values$_GET & .htaccess | PHP 忽略值
【发布时间】:2020-04-14 18:11:42
【问题描述】:

我遇到了这个问题,我尝试制作一个图像存档。大部分都有效,但我现在开始美化 URL。我以前用过这个,但不知何故,它现在不起作用。如果我向同一个文件(索引)添加多个 Rewrite URL,它会忽略添加的值。

我得到了什么

Header add "disablevcache" "true"

RewriteEngine On
RewriteBase /folder/
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteRule ^archive/([^/]+)?$                              archive/index.php?cat=$1 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)?$                      archive/index.php?cat=$1&sub=$2 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?$              archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?/([^/]+)$      archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]

以下内容应该允许我访问这些 URL:

domain.com/archive/category
domain.com/archive/category/subcategory/
domain.com/archive/category/subcategory/id
domain.com/archive/category/subcategory/id/item

domain.com/archive 将被设为默认值,因为我在文件夹中使用 index.php 文件。项目将只用于 SEO,与 PHP 无关。 (最后的rewriteRule)

这行得通。我可以从所有不同的 URL 访问同一个页面。 (存档/index.php)。但是当我尝试使用 PHP 从提供的 URL 中获取值时,它不起作用。我什至无法检查是否有值。

无论我做什么,它都会在每个 URL 上回显类别和子类别。另外,如果我转到默认 URL,domain.com/archive。

if($_GET['cat']){

    echo 'Category';

    if($_GET['sub']){

        echo 'Sub Category';
    }
}

任何人都可以看到我做错了什么,或者得到了关于它为什么忽略我的价值观的建议?或者也许知道另一种方法来做到这一点,所以我最终得到了相同的结果?

【问题讨论】:

  • 看起来 Apache 忽略了第二个和其他规则,因为它与第一个匹配。如果您删除第一条规则然后尝试访问 domain.com/archive/category/subcategory/ 会发生什么?
  • @harisdev 如果我删除它们并保留一条规则,它将起作用。已经尝试过了——但需要 4 条规则。从我在这里可以看到,stackoverflow.com/questions/20468460/… 就是这样做的方法。
  • 尝试颠倒规则,从最长的第一个开始,最短的结束
  • @delboy1978uk 遗憾的是没有区别:-/
  • 这部分是什么? [E=ORIG_URI:/$1] 你试过了吗(再次以相反的顺序添加[L,NC](L 是最后一个,匹配时不会进一步处理,NC 不区分大小写)

标签: php .htaccess


【解决方案1】:

Apache 循环重写。您需要为 archive/index.php 停止它,因为它始终与第一个 RewriteRule 匹配:

RewriteRule ^archive/index\.php$                            - [L]
RewriteRule ^archive/([^/]+)?$                              archive/index.php?cat=$1 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)?$                      archive/index.php?cat=$1&sub=$2 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?$              archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?/([^/]+)$      archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]

[E=ORIG_URI:/$1]不使用也可以删除。

【讨论】:

  • 非常感谢!
  • 不知道 [E=ORIG_URI:/$1] 做什么,tbh。
  • @lol25500 不客气!它创建了一个以后可以使用的变量。如果您不使用该变量,则可以将其删除。
猜你喜欢
  • 1970-01-01
  • 2015-06-30
  • 1970-01-01
  • 2011-05-16
  • 2015-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-25
相关资源
最近更新 更多