【问题标题】:Specific .htaccess Redirect with a wildcard in the middle特定的 .htaccess 重定向,中间带有通配符
【发布时间】:2013-09-09 23:49:10
【问题描述】:

我会先说类似主题的其他帖子无法帮助我解决这个确切的问题。

我正在尝试重写以下内容:

原创
mydomain.com/brand-collection/{wildcard1}/{wildcard2}.html

重写为:
mydomain.com/{wildcard2}.html

示例 #1:
mydomain.com/brand-collection/sony/sony-xperia.html"
将改写为:
mydomain.com/sony-xperia.html

示例 #2:
mydomain.com/brand-collection/sharp/sharp-aquos.html"
将改写为:
mydomain.com/sharp-aquos.html

非常感谢任何帮助。谢谢!

【问题讨论】:

  • 你有类似的我可以找到答案的吗?

标签: regex .htaccess mod-rewrite wildcard


【解决方案1】:

如果您想“重写”,则意味着浏览器不受影响,并且 URI 在服务器端完全更改。您希望在文档根目录的 htaccess 文件中有类似的内容:

RewriteEngine on
RewriteRule ^brand-collection/[^/]+/([^/.]+)\.html$ /$1.html [L]

所以如果浏览器请求:

http://mydomain.com/brand-collection/sony/sony-xperia.html

它得到的是这里的文件:

http://mydomain.com/sony-xperia.html

如果没有 /sony-xperia.html 文件,那么您只会得到 404。

如果要重定向浏览器,只需将R 标志添加到方括号中即可。

【讨论】:

  • @ThatFaucetGuy 它取决于 magento 规则。以及应该如何处理/sony-xperia.html url。它们是由 magento 处理的页面吗?它们是静态 HTML 页面吗?您是否尝试过在您的 magento 规则之前或之后通过发出品牌收集规则来重新排序规则?
  • 对不起,我现在才看到你的第一个回复。我把它放在一个非 magento 网站上,让它破坏了原来的 url + 新的 url,我能做些什么来测试?谢谢!
  • 我从答案中找出了我的问题。它实际上与我需要的相反。如果您访问http://mydomain.com/sony-xperia.html,我需要它来提供位于http://mydomain.com/brand-collection/sony/sony-xperia.html 的文件,您能帮我做相反的事情吗?谢谢,我知道这将是修复
  • @ThatFaucetGuy 在sony 之后的brand-collection 来自哪里?它总是会是/sony-xperia.html URI 中的第一个单词吗?
  • 我的结构将是 mydomain.com/brand-collection/[wildcard_directory]/[wildcard-page].html 所以 mydomain.com/brand-collection 是不变的,但 /sony/sony-xperia.html` 可以是其他任何东西。例如它可以是/samsung/samsung-galaxy.html/frito-lay/doritos-cool-ranch.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-04
  • 2018-10-03
  • 1970-01-01
相关资源
最近更新 更多