简体   繁体   English

如何使用 Laravel 中的 .htaccess 重定向具有相同查询字符串的域

[英]How to redirect domain with the same query string using .htaccess in Laravel

I am trying to redirect domain with the same path (query string) to another domain.我正在尝试将具有相同路径(查询字符串)的域重定向到另一个域。 In frontend we are using Angular app and backend is Laravel .在前端,我们使用Angular应用程序,后端是Laravel I have tried with the .htaccess below but it doesn't use the path.我试过下面的.htaccess但它没有使用路径。 It just redirects to index.php page .它只是重定向到 index.php 页面

  • I want this: xxx.com/this-is/the-path -> yyy.com/this-is/the-path我想要这个:xxx.com/this-is/the-path -> yyy.com/this-is/the-path
  • But I get this: xxx.com/this-is/the-path -> yyy.com/index.php但我明白了:xxx.com/this-is/the-path -> yyy.com/index.php

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

# -- REDIRECTION to https (optional):
# If you need this, uncomment the next two commands
# RewriteCond %{HTTPS} !on
# RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# --

# CustomRedirect start
RewriteCond %{HTTP_HOST} ^xxx.io$ [OR]
RewriteCond %{HTTP_HOST} ^yyy.lt$
RewriteRule ^(.*)$ https://test.lt/$1 [L,QSA,R=302]
# CustomRedirect end

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d

RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) index.html [NC,L]

# CustomRedirect start RewriteCond %{HTTP_HOST} ^xxx.io$ [OR] RewriteCond %{HTTP_HOST} ^yyy.lt$ RewriteRule ^(.*)$ https://test.lt/$1 [L,QSA,R=302] # CustomRedirect end

You've put this redirect rule in the wrong place.您将此重定向规则放在了错误的位置。 This needs to go at the top of the file.这需要文件顶部的 go。 You've placed it after the internal rewrite to index.php (the Laravel front-controller), so it naturally ends up redirecting to index.php , not the URL-path you are requesting.您在内部重写index.php (Laravel 前端控制器)之后将其放置,因此它自然会最终重定向到index.php ,而不是您请求的 URL 路径。

(It would only redirect as intended if you requested a physical file or directory at one of the other domains, since the request would not be rewritten to index.php in this case.) (如果您请求其他域之一的物理文件或目录,它只会按预期重定向,因为在这种情况下请求不会被重写为index.php 。)

The QSA flag is also superfluous here. QSA标志在这里也是多余的。 The query string is appended by default.默认情况下附加查询字符串。


Aside:在旁边:

with the same path (query string)具有相同的路径(查询字符串)

The "path" part of the URL is not the "query string". URL 的“路径”部分不是“查询字符串”。 Your examples contain a URL-path only, there is no "query string" in the examples you've posted.您的示例仅包含 URL 路径,您发布的示例中没有“查询字符串”。 The query string is the part of the URL following the first ?查询字符串是 URL 中第一个? (question mark). (问号)。 However, the above rule will preserve both the URL-path and the query string (if any).但是,上述规则将保留 URL 路径和查询字符串(如果有)。

 # Handle Authorization Header RewriteCond %{HTTP:Authorization}. RewriteRule.* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

This rule is also in the wrong place.这条规则也用错了地方。 This needs to go before the # Handle Front Controller... rule, not after it.这需要 go# Handle Front Controller...规则之前,而不是在它之后。 Currently, this rule is not doing anything (maybe you don't need it).目前,这条规则没有做任何事情(也许你不需要它)。

 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^(.*) index.html [NC,L]

Likewise, this rule block (at the end of the file) is also not doing anything.同样,这个规则块(在文件末尾)也没有做任何事情。 The request will be rewritten to index.php (the Laravel front-controller) first.该请求将首先被重写为index.php (Laravel 前端控制器)。 (You cannot do both.) (你不能两者都做。)

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

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