如何修改这个.htaccess规则以适应两种情况?

huangapple go评论58阅读模式
英文:

How do I modify this .htaccess rule so it can accommodate two scenarios?

问题

以下的.htaccess规则将URL中的"OLDNAME"替换为"NEWNAME",并且如果存在的话,它会从URL末尾移除字符串"-2"。我需要更新规则以适应两种情况:"OLDNAME1"和"OLDNAME2"。两者都重定向到单一的"NEWNAME"。

RewriteRule ^(index.php/)?(OLDNAME1|OLDNAME2)/(.+?)(?:-2)?/?$ /NEWNAME/$3 [L,NC,R=302,NE]



<details>
<summary>英文:</summary>

The following .htaccess rule replaces &quot;OLDNAME&quot; with &quot;NEWNAME&quot; in a URL and it removes the string &quot;-2&quot; from the end of the URL if it exists. I need to update the rule to accommodate two scenarios: &quot;OLDNAME1&quot; and &quot;OLDNAME2&quot;. Both redirect to a single &quot;NEWNAME&quot;.

RewriteRule ^(index.php/)?OLDNAME/(.+?)(?:-2)?/?$ /NEWNAME/$2 [L,NC,R=302,NE]



I&#39;ve tried several variations of the following:

RewriteRule ^(index.php/)?(OLDNAME1|OLDNAME2)/(.+?)(?:-2)?/?$ /NEWNAME/$2 [L,NC,R=302,NE]


Any help appreciated.

</details>


# 答案1
**得分**: 1

使用您提供的示例,请尝试以下`.htaccess`规则文件。我们需要在这里使用`THE_REQUEST`变量,并在测试URL之前确保清除浏览器缓存。

```apache
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/?index\.php/?\?(?:OLDNAME[12])(.+?)(?:-2)\s [NC]
RewriteRule ^ /NEWNAME/%1? [R=301,L]
英文:

With your shown samples please try following .htaccess rules file. We need to use THE_REQUEST variable here and also make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/?index\.php/?\?(?:OLDNAME[12])(.+?)(?:-2)\s [NC]
RewriteRule ^ /NEWNAME/%1? [R=301,L]

答案2

得分: 1

您非常接近,可以这样做:

# /nwblog =&gt; /blog
RewriteRule ^(?:index\.php/)?nwblog/(.+?)(?:-2)?/?$ /blog/$1 [L,NC,R=302,NE]

# 移除 /index.php/
RewriteRule ^index\.php/(.+)$ /$1 [L,NC,R=302,NE]

# 移除 /-2
RewriteRule ^(.+)-2/?$ /$1 [L,NC,R=302,NE]
英文:

You are pretty close, you can do like this:

# /nwblog =&gt; /blog
RewriteRule ^(?:index\.php/)?nwblog/(.+?)(?:-2)?/?$ /blog/$1 [L,NC,R=302,NE]

# remove /index.php/
RewriteRule ^index\.php/(.+)$ /$1 [L,NC,R=302,NE]

# remove /-2
RewriteRule ^(.+)-2/?$ /$1 [L,NC,R=302,NE]

huangapple
  • 本文由 发表于 2023年3月7日 23:19:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定