英文:
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 "OLDNAME" with "NEWNAME" in a URL and it removes the string "-2" from the end of the URL if it exists. I need to update the rule to accommodate two scenarios: "OLDNAME1" and "OLDNAME2". Both redirect to a single "NEWNAME".
RewriteRule ^(index.php/)?OLDNAME/(.+?)(?:-2)?/?$ /NEWNAME/$2 [L,NC,R=302,NE]
I'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 => /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 => /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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论