【问题标题】:.htaccess to redirect specific index file when url is visited.htaccess 在访问 url 时重定向特定的索引文件
【发布时间】:2014-12-27 00:36:26
【问题描述】:

我的产品网址如下所示:http://www.example.com/products/handbags/leather-handbag-model-ba123 其中handbags 是类别名称,它是动态变化的。 leather-handbag-model-ba123 是产品唯一的 url,并且也在动态变化。
.htaccess 文件中我有这个:

RewriteCond %{THE_REQUEST} /(products)/([^\s&]+)/([^\s&]+) [NC]
RewriteRule /%1/%2/index.php [L] 

应该加载位于 /products/handbags/index.php 中的 index.php 文件,但代码返回错误 404。
将加载特定类别的 index.php 文件(示例中为handbags)的正确代码是什么?

【问题讨论】:

  • 你在“products/handbags”中有一个文件“index.php”吗?如果你不...那么这就是你得到 404 的原因。你需要很多文件夹,其中包含“index.php”。那将是...每个类别 1 个文件夹...
  • 是的,我在每个分类文件夹中都有 index.php

标签: php apache .htaccess redirect


【解决方案1】:

用途:

RewriteCond %{REQUEST_URI} !index\.php$ [NC]
RewriteRule ^(products)/([^/]+)/.+/? /$1/$2/index.php [L]

【讨论】:

  • 您使用 THE_REQUEST 来避免与其他 RewriteRule 循环?
  • 如果我有这个 RewriteRule 。 ./products/Handbags/index.php [L] 重定向有效,但它固定在 Handbags 文件夹中
  • 但是用这个试试 [R=302,L,NE] 看看正确的 URL
  • 在测试了您的建议后,我发现这是错误的: RewriteCond %{THE_REQUEST} /(products)/([^\s&]+)/[^\s&]+ [NC]跨度>
  • 如果您尝试重定向索引,请尝试使用 RewriteRule ^$ /products/Handbags/index.php [L] 进行其他操作。为什么错???
猜你喜欢
  • 1970-01-01
  • 2014-02-01
  • 1970-01-01
  • 2015-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多