【问题标题】:.htaccess - rewrite multiple links (wildcard).htaccess - 重写多个链接(通配符)
【发布时间】:2017-01-24 19:11:34
【问题描述】:

我有大量链接 - 示例:

tempfile-1.php 
tempfile-2.php 
tempfile-3.php 
... 
tempfile-255.php

我需要做的是将文件分别重定向到:

temp-file-1.php 
temp-file-2.php 
temp-file-3.php 
... 
temp-file-255.php

我知道如何一一重写,但这不是解决方案。 你能帮忙在通配符规则/条件中重写这些类型的链接吗? 非常感谢。

【问题讨论】:

  • @anubhava - 请您看一下这个
  • 使用正则表达式。这方面的文章很多。即searchenginepeople.com/blog/…
  • 是的,这个话题有很多文章,仍然有问题要解决
  • 重写还是重定向?
  • 我不知道 mod-rewrite,但我可以给你 sed 中的正则表达式。显然,“temp”或“file”必须是一个常数。我假设常量是“temp”而不是“file”。这个表达式 ... sed 's/^temp(.*-)(.*).php/temp-\1\2/' ... 会做你想做的事。 $ cat links.txt tempfile-1.php tempfile-2.php tempfile-3.php tempfile-255.php $ cat links.txt | sed 's/^temp(.*-)(.*).php/temp-\1\2/' temp-file-1 temp-file-2 temp-file-3 temp-file-255

标签: regex apache .htaccess url mod-rewrite


【解决方案1】:

假设关键字file 出现在所有链接中,您可以在站点根目录.htaccess 中使用这个单一的通用重定向规则来重定向所有链接:

RewriteEngine On

RewriteRule ^(temp)(file-.+\.php)$ /$1-$2 [L,NC,R=301]
  • (temp.+) 是第一个匹配从 temp 开始的组
  • file-\d+\.php 是第二个捕获的组,以 file 开头并以 .php 结尾

【讨论】:

  • 你好。抱歉迟了回应。指定“temp”对我来说很重要,因为有类似的文件组。我只需要解决这个“临时”组。我也没有指定在“file-”之后不仅是数字,而且是字母,所以 d+ 仅适用于数字。我想出了这样的东西,但似乎没有用。 ...... RewriteRule (/^temp/)(file-[a-zA-Z0-9]\.php)$ /$1-$2 [L,NC,R=301]
【解决方案2】:

这样试试,正常工作时可以把R=302改成R=301

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)(file-\d+).php$ $1-file-$2.php [R=302,L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2014-03-11
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 2016-04-09
    • 2017-05-09
    相关资源
    最近更新 更多