简体   繁体   English

使用 htaccess 将 301 从子域重定向到子文件夹/使用不同的 URL

[英]Redirecting 301 from SubDomain to SubFolder / with diffrent URLs using htaccess

i have a web with this address(subdomain): subd.example.com and want to redirect it to (subfolder): https://www.example.com/subf/我有一个带有此地址的网站(子域): subd.example.com并希望将其重定向到(子文件夹): https://www.example.com/subf/ : subd.example.com

is I searched the code bellow works if the urls in my websites wont change:如果我的网站中的 url 不会改变,我是否搜索了下面的代码:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^subd\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/subf/$1/ [R=301]

but some or most of my URLs will be changed for example from /oldlink1 to /newlink1 and ...但是我的部分或大部分 URL 将被更改,例如从 /oldlink1 更改为 /newlink1 和...

somehow im stuck and the codes wont work, only home page redirects correctly, i tried:不知何故我卡住了,代码不起作用,只有主页重定向正确,我试过:

Redirect 301 /oldlink1 www.example.com/subf/newlink1

but it doesnt work, returns me this link: www.example.com/subf/oldlink但它不起作用,返回给我这个链接: www.example.com/subf/oldlink

any help please?有什么帮助吗?

EDIT: if it is important both subdomain and subfolder names are the same, I mean subd and subf are both sub .编辑:如果是重要的两个子域名和子文件夹名称是相同的,我的意思是SUBDsubf都是sub addresses are:地址是:

sub.example.com
www.example.com/sub/

Your issue most likely is the position of those specific redirection rules.您的问题很可能是这些特定重定向规则的位置。 This is important, you need to consider that the rule set is processed top to bottom.这很重要,您需要考虑规则集是从上到下处理的。 So you need to implement specific exceptions first, so further up, and more general rules later, so further down.所以你需要先实现特定的例外,然后再往上,然后再实现更多的通用规则,再往下。

This probably is what you are looking for:这可能就是你要找的:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^subd\.example\.com$
RewriteRule ^/?$ https://example.com/subf [R=301,L]

RewriteCond %{HTTP_HOST} ^subd\.example\.com$
RewriteRule ^/?oldlink1/?$ https://example.com/subf/newlink1 [R=301,L]

RewriteCond %{HTTP_HOST} ^subd\.example\.com$
RewriteRule ^/?oldlink2/?$ https://example.com/subf/newlink2 [R=301,L]

RewriteCond %{HTTP_HOST} ^subd\.example\.com$
RewriteRule ^/?oldlink3/?$ https://example.com/subf/newlink3 [R=301,L]

RewriteCond %{HTTP_HOST} ^subd\.example\.com$
RewriteRule ^/?(.*)/?$ https://example.com/subf/$1 [R=301,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM