如何使用.htaccess更改文件夹名称并从URL末尾删除字符串。

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

How to use htaccess to change a folder name and remove a string from the end of a URL

问题

我的目标

我正在尝试使用htaccess来更改像这样的URL...

https://example.com/OLDNAME/item/a-long-file-name-separated-by-hyphens-2

https://example.com/OLDNAME/item/a-long-file-name-separated-by-hyphens

变成像这样的URL

https://example.com/NEWNAME/item/a-long-file-name-separated-by-hyphens

步骤

  1. 将URL中的OLDNAME替换为NEWNAME。每当在URL中找到'OLDNAME'时,此规则都应执行。

  2. 当URL包含'OLDNAME'时,它有时在URL末尾有字符串'-2'。如果找到此'-2'字符串,也需要将其删除。

我尝试过的

我已经使用以下规则使重定向从OLDNAMENEWNAME工作...

RewriteRule ^OLDNAME/(.*)$ /NEWNAME/$1 [NC]

我已经阅读了许多帖子和mod_rewrite文档的许多页面,但尚未在尝试删除'-2'时取得太大的进展。

英文:

My Goal

I'm trying to use htaccess to change URLs like this...

https://example.com/OLDNAME/item/a-long-file-name-separated-by-hyphens-2

https://example.com/OLDNAME/item/a-long-file-name-separated-by-hyphens

Into URLs like this

https://example.com/NEWNAME/item/a-long-file-name-separated-by-hyphens

The Steps

  1. Replace OLDNAME in the URL with NEWNAME. This rule should execute anytime 'OLDNAME' is found in the URL.

  2. When a URL contains 'OLDNAME', it sometimes has the string '-2' at the end of the URL. If this '-2' string is found, it needs to be removed too.

What I've tried

I have the redirect from OLDNAME to NEWNAME working using this rule...

RewriteRule ^OLDNAME/(.*)$ /NEWNAME/$1 [NC]

I've read many posts and lots of pages from mod_rewrite documentation, but I haven't made much progress trying to remove the -2 yet.

Any help appreciated!

答案1

得分: 2

你可以使用以下规则:

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

我假设你想要重定向到新的URL,因此添加了R标志。

请注意,在RewriteRule中以-2结尾的模式位于捕获组之外。当我们在目标中使用$1时,它将不包括最后的-2

测试输出:

curl -I 'localhost/OLDNAME/item/a-long-file-name-separated-by-hyphens'
HTTP/1.1 302 Found
Date: Fri, 03 Mar 2023 20:48:50 GMT
Server: Apache/2.4.54 (Unix) OpenSSL/1.1.1s PHP/8.1.12
Location: http://localhost/NEWNAME/item/a-long-file-name-separated-by-hyphens
Content-Type: text/html; charset=iso-8859-1

curl -I 'localhost/OLDNAME/item/a-long-file-name-separated-by-hyphens-2'
HTTP/1.1 302 Found
Date: Fri, 03 Mar 2023 20:49:53 GMT
Server: Apache/2.4.54 (Unix) OpenSSL/1.1.1s PHP/8.1.12
Location: http://localhost/NEWNAME/item/a-long-file-name-separated-by-hyphens
Content-Type: text/html; charset=iso-8859-1
英文:

You may use this rule:

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

I assume you want to redirect to new URL hence added R flag.

Note pattern in RewriteRule that ends with -2 is outside the capture group. When we use $1 in target it will be without last -2.

Test Output:

curl -I 'localhost/OLDNAME/item/a-long-file-name-separated-by-hyphens'
HTTP/1.1 302 Found
Date: Fri, 03 Mar 2023 20:48:50 GMT
Server: Apache/2.4.54 (Unix) OpenSSL/1.1.1s PHP/8.1.12
Location: http://localhost/NEWNAME/item/a-long-file-name-separated-by-hyphens
Content-Type: text/html; charset=iso-8859-1

curl -I 'localhost/OLDNAME/item/a-long-file-name-separated-by-hyphens-2'
HTTP/1.1 302 Found
Date: Fri, 03 Mar 2023 20:49:53 GMT
Server: Apache/2.4.54 (Unix) OpenSSL/1.1.1s PHP/8.1.12
Location: http://localhost/NEWNAME/item/a-long-file-name-separated-by-hyphens
Content-Type: text/html; charset=iso-8859-1

huangapple
  • 本文由 发表于 2023年3月4日 00:04:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629361.html
匿名

发表评论

匿名网友

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

确定