最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - htaccess redirect url by last segment - Stack Overflow

programmeradmin0浏览0评论

I'm transferring my old site to a new shopify domain.

What I'm looking for is to redirect from here


to there:


I've created this regex (?<=\/)\w+$

and checked in : it matches the «brand».

I checked the .htaccess in


RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/golf
RewriteRule "(?<=\/)\w+$" "/$1" [R=301,L]

and the output URL is:

://url-old/collections/

The output contains the old URL without the brand AND the new URL, but $1 is not defined or used.

I've tried and looked a lot. I hope, somebody can help me out.

I'm transferring my old site to a new shopify domain.

What I'm looking for is to redirect from here

https://www.url-old.com/golf/shoes/white/winter/brand

to there:

https://url-old.com/collections/brand

I've created this regex (?<=\/)\w+$

and checked in https://regexr.com : it matches the «brand».

I checked the .htaccess in https://htaccess.madewithlove.com

https://www.url-old.com/golf/shoes/white/winter/brand
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/golf
RewriteRule "(?<=\/)\w+$" "https://url-old.com/collections/$1" [R=301,L]

and the output URL is:

https://www.url-old.com/golf/shoes/white/winter/https://url-old.com/collections/

The output contains the old URL without the brand AND the new URL, but $1 is not defined or used.

I've tried and looked a lot. I hope, somebody can help me out.

Share Improve this question edited Feb 6 at 17:42 MrWhite 45.8k8 gold badges64 silver badges87 bronze badges asked Feb 6 at 12:44 Thomas HenkeThomas Henke 91 silver badge2 bronze badges 5
  • It probably thinks the substitution is supposed to be a relative URL path, because you put it into double quotes. Why use a full URL to begin with, when you're not trying to redirect to a different origin, why not just /collections/$1? – C3roe Commented Feb 6 at 12:53
  • Assuming you'll always have at least one path segment between golf and the brand, RewriteRule ^golf/.*/([^/]+)$ /collections/$1 [R=301,L] should basically already do the job, htaccess.madewithlove.com?share=f718a017-d0d8-4c1c-86a7-5828b84… (No RewriteCond, because the RewriteRule itself is perfectly capable of applying that part of the condition to the URL path via its own pattern.) – C3roe Commented Feb 6 at 13:04
  • @C3roe thanks for your help. I still had issues due to the line RewriteRule ^index.php/(.*)$ /$1 [R=302,L] prior to your ^golf/.*/([^/]+)$ after changing the order, it works. thank you – Thomas Henke Commented Feb 6 at 16:11
  • from @agent-j This should do the trick: [^/]+(?=/$|$) With a (?=lookahead) you won't get the last slash. [^/]+ Looks for at least one character that is not a slash (as many as possible). (?=/?^|^) makes sure that the next part of the string is a / and then the end of string or just end of string. Matches match in /one/two/match, '/one/two/match/'. => ^golf/.*/([^/]+(?=/$|$)) /collections/$1 [R=301,L] – Thomas Henke Commented Feb 6 at 16:55
  • "I checked the .htaccess in htaccess.madewithlove.com" - I think it's worth pointing out that the MWL tester is completely wrong in this case. (The double quotes are entirely optional here - they are not the source of error.) No need for any lookaround-assertions. – MrWhite Commented Feb 6 at 17:56
Add a comment  | 

1 Answer 1

Reset to default 0

try

RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/golf/shoes/white/winter/(.*)$
RewriteRule ^ /collections/%1 [R=301,L]

This Should correctly redirect https://www.url-old.com/golf/shoes/white/winter/brand to https://url-old.com/collections/brand.

发布评论

评论列表(0)

  1. 暂无评论