Matching specific host name and redirecting to www.myhostname.com/some-extra-path, not affecting other hostnames

柔情痞子 提交于 2021-01-29 14:29:15

问题


I'm having no luck googling this, maybe because I'm using the wrong terminology.

Here are some examples to clarify the behavior I'm looking for:

Redirects for

  • hostname-two.com -> hostname-two.com/sub-folder

No re-directs for

  • hostname-two.com/something (URL already ok url)

  • hostname-two.com/sub-folder (URL already ok url)

  • hostname-two.com/sub-folder/something (URL already ok url)

  • hostname-one.com (hostname-one should not be affected)

  • hostname-one.com/something (hostname-one should not be affected)

So I'm thinking no URL rewrites should be needed, only redirects.

Current setup

I have a server that responds to hostname-one.com and hostname-two.com

This is accomplished with two server blocks with different server_name properties.

What I have tried

I have tried using location blocks in the server block that has the server_name of hostname-two.com but my attempts so far hasn't been able to satisfy all the conditions above.

I would be very grateful if someone could point me in the right direction!


回答1:


You can try to use this location in your second server block:

location = / {
    return 301 /sub-folder;
}

You still can use location / { ... } block, it will process any other request:

location = / {
    return 301 /sub-folder;
}
location / {
    ...
}


来源:https://stackoverflow.com/questions/60551733/matching-specific-host-name-and-redirecting-to-www-myhostname-com-some-extra-pat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!