URL 重写以根据关键字更改 URL 的特定部分

问题描述 投票:0回答:1

每当对服务器有这样的请求

https://%{HTTP_HOST}/app/accounturi/services

我想重定向到

https://%{HTTP_HOST}/appLegacy/accounturi/services

在这里

accounturi
将是一个动态的。我正在 apache2 中尝试这个。

我尝试了下面的方法并坚持使用 RewriteRule。

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.*)services/?$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/appLegacy/
ubuntu mod-rewrite apache2
1个回答
0
投票
RewriteCond %{REQUEST_URI} ^/(.*)services/?$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/appLegacy/

这将 302(临时)将任何以

services
services/
结尾的 URL 重定向到
https://<hostname>/appLegacy/
,丢失尾随的
accounturi/services
部分。

你也没有在 URL 路径的开头检查

/app
? (这让我觉得你的
.htaccess
文件在
/app
子目录中?但是没有别的建议。)

如果

.htaccess
文件位于
/app
子目录中那么规则应该是这样的:

RewriteRule ^([^/]+/services)/?$ /appLegacy/$1 [R=302,L]

前面的

RewriteCond
指令不是必需的。
$1
反向引用包含从
RewriteRule
pattern 捕获的 URL 路径(这不包括请求中可能出现的任何可选尾部斜杠)。请注意,由于
.htaccess
文件位于
/app
子目录中,因此它被排除在捕获的 URL 路径之外。

您不需要指定绝对 URL 作为目标,因为您似乎重定向到同一个 host。但是,您可以选择包含它以强制执行 HTTPS。

但是,如果

.htaccess
文件位于文档根目录中,那么指令需要看起来像这样:

RewriteRule ^app/([^/]+/services)/?$ /appLegacy/$1 [R=302,L]
© www.soinside.com 2019 - 2024. All rights reserved.