【问题标题】:mod_rewrite Replace underscores with plus sign and pass data to new URLmod_rewrite 用加号替换下划线并将数据传递到新 URL
【发布时间】:2015-11-10 18:35:47
【问题描述】:

我最近切换到 Wordpress,我不想丢失一些旧页面。 旧网站的页面使用以下 URL 格式

domain.name/gallery/word1_word2_word3_wordX.html

我需要将这些链接转换为:

domain.name/?s=word1+word2+word3+wordX&post_type=product

基本上我需要在 gallery/ 之后获取所有内容,删除 .html,用加号替换下划线并将其传递给 domain.name/?s= 和 &post_type=product 之间的新 URL 格式

感谢任何帮助。 谢谢。

【问题讨论】:

  • 其实这是我尝试针对我的情况修改的第一个方法,但是我的mod_rewrite能力非常有限,结果没有成功。

标签: .htaccess mod-rewrite


【解决方案1】:

我玩了一下,想出了一个可行的解决方案:

RewriteEngine On

# Note: This line is important! Without it, your URLs will look like example.com/home/you/public_html/gallery/etc, which is bad.
RewriteBase /

# Replace any _ with + and redirect
RewriteRule ^gallery\/(.*)_(.*) gallery/$1+$2 [R=301]
# Replace gallery/whatever.html with /?s=whatever&post_type=product
RewriteRule ^gallery/(.*)\.html /?s=$1&post_type=product [R=301]

这样转

http://example.com/gallery/foo_bar_baz_bam.html

进入

http://example.com/?s=foo+bar+baz+bam&post_type=product

我利用this postthis one 提出了这个建议;您可能会发现这些也很有帮助。

注意:这会对重写的每个阶段进行重定向(每个_ 一个,一个用于最终转换)。从理论上讲,可以通过在第一个 RewriteRule 中使用 [N] 而不是 [R=301].htaccess 等效于 while 循环)来进行一次重定向,但是如果不创建,我将无法完成此工作一个无限循环。

【讨论】:

  • 谢谢!这就像一个魅力。我还学到了一些关于 mod_rewrite 的新东西,这总是一件好事。
  • @proone 很高兴为您提供帮助!
猜你喜欢
  • 2010-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-24
  • 2013-03-22
相关资源
最近更新 更多