【问题标题】:Redirect users to content outside Angular but as a subpath of the application将用户重定向到 Angular 之外的内容,但作为应用程序的子路径
【发布时间】:2020-12-03 15:43:58
【问题描述】:

我目前有一个使用 Angular 的主网站,以及一些作为子域的登录页面,使用纯 HTML/JS。
有人要求我现在应该使用website.com/lp,而不是lp.website.com

考虑到所有页面和 Angular 应用程序的文件夹都在 Apache 下的同一台服务器上,我如何将用户重定向到适当的内容,但保留 website.com/lp URL?

【问题讨论】:

    标签: angular apache redirect subdomain


    【解决方案1】:

    如果您在 .htaccess 文件中创建重写规则,则可以创建别名。这样,您可以保持当前结构完全不变,直到您可以逐步淘汰子域登录页面。

    RewriteEngine on
    
    #Make sure we only rewrite for domains example.com and www.example.com
    RewriteCond ${HTTP_HOST} ^(www\.)?example.com$
    
    # If the request URI is either /lp or /lp/ do an internal redirect
    # (The leading slash of the request URI is stripped before matching)
    RewriteRule ^lp/?$ https://lp.example.com/
    

    RewriteRule 执行以下操作:

    用户请求https://example.com/lp/,但在重写服务器后“认为”用户请求https://lp.example.com/

    这是一个相反的例子。

    # Make sure we only rewrite for the lp.example.com domain
    RewriteCond ${HTTP_HOST} ^lp.example.com$
    # Rewrite all root requests to https://example.com/lp/
    RewriteRule ^/?$ https://example.com/lp/
    

    服务器收到https://lp.example.com 的请求。重写条件确保重写只发生在 lp.example.com。 RewriteRule 内部重定向到http://example.com/lp/

    更多信息https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule

    【讨论】:

    • 我不太了解 .htaccess 语法,但请您详细说明最后一行的作用?截至今天,我所有的文件都在子域 URL 的目录中。我需要的是,无论用户何时输入子域或子路径 URL,它们都会显示相同的内容。
    猜你喜欢
    • 2022-06-13
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 2022-08-24
    • 2016-05-27
    • 1970-01-01
    • 2021-04-15
    相关资源
    最近更新 更多