匹配精确字符串长度时重定向URL的.htaccess?

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

htaccess redirect url if matching exact string length?

问题

以下是要翻译的内容:

"I have a lot of old urls inbound pointing to incorrect locations, trying to forward to new location. These are going to the root directory so I can't just forward everything.

One way to get a good chunk of them on to the new place is finding ones with a session ID in the query string. It always has 32 characters, preceded by s=

https://www.example.com/some-url-name-1233/?s=ba4a8a734b666b8d43499e5d497599a6

Need to move that to (and drop the session ID)

https://www.example.com/newfolder/some-url-name-1233/

I can't get the .htaccess redirect to match that string.

I've tried multiple ways, most recent being:

RewriteRule ^(.*)s=([^.]{32})$ https://www.example.com/newfolder/$1 [L,R=301]

Any suggestions?"

英文:

I have a lot of old urls inbound pointing to incorrect locations, trying to forward to new location. These are going to the root directory so I can't just forward everything.

One way to get a good chunk of them on to the new place is finding ones with a session ID in the query string. It always has 32 characters, preceded by s=

https://www.example.com/some-url-name-1233/?s=ba4a8a734b666b8d43499e5d497599a6

Need to move that to (and drop the session ID)

https://www.example.com/newfolder/some-url-name-1233/

I can't get the .htaccess redirect to match that string.

I've tried multiple ways, most recent being:

RewriteRule ^(.*)s=([^.]{32})$ https://www.example.com/newfolder/$1 [L,R=301]

Any suggestions?

答案1

得分: 1

这是一个经常出现的、充分记录的问题:你不能通过 RewriteRule 访问请求的查询字符串。你需要使用 RewriteCond 来实现这个目的:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^s=[^&]{32}(&|$)
RewriteRule ^ https://www.example.com/newfolder%{REQUEST_URI} [L,R=301,QSD]

我还修复了一些其他细节。

英文:

This is an often answere, fully documented issue: you cannot access a request's query string by means of a RewriteRule. You need to use a RewriteCond for that:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^s=[^&]{32}(&|$)
RewriteRule ^ https://www.example.com/newfolder%{REQUEST_URI} [L,R=301,QSD]

I also fixed some other details.

huangapple
  • 本文由 发表于 2023年2月8日 09:15:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380512.html
匿名

发表评论

匿名网友

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

确定