英文:
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
步骤
-
将URL中的
OLDNAME
替换为NEWNAME
。每当在URL中找到'OLDNAME'时,此规则都应执行。 -
当URL包含'OLDNAME'时,它有时在URL末尾有字符串'-2'。如果找到此'-2'字符串,也需要将其删除。
我尝试过的
我已经使用以下规则使重定向从OLDNAME
到NEWNAME
工作...
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
-
Replace
OLDNAME
in the URL withNEWNAME
. This rule should execute anytime 'OLDNAME' is found in the URL. -
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论