【问题标题】:Redirect in Laravel .htaccess在 Laravel 中重定向 .htaccess
【发布时间】:2020-05-15 20:59:42
【问题描述】:

我是 Laravel 的初学者。我在我的项目中使用 Laravel 5.8。 我做新网站(旧网站被删除)

我有 .htaccess:

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

    RewriteEngine On
    # Redirect to https
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://domain.pl/$1 [R,L]

    # Redirect to non www
    RewriteCond %{HTTP_HOST} ^www.domain.pl$ [NC]
    RewriteRule ^(.*)$ https://domain.pl/$1 [R=301,L]

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

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

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

    # Fonts
    # Add correct content-type for fonts
    AddType application/vnd.ms-fontobject .eot
    AddType application/x-font-ttf .ttf
    AddType application/x-font-opentype .otf
    AddType application/x-font-woff .woff
    AddType image/svg+xml .svg

    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 month"
    ExpiresByType image/ico "access plus 1 month"

    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/css "now plus 1 month"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
    ExpiresByType application/x-font-ttf "access plus 1 month"
    ExpiresByType application/x-font-opentype "access plus 1 month"
    ExpiresByType application/x-font-woff "access plus 1 month"
    ExpiresByType image/svg+xml "access plus 1 month"
    ExpiresByType text/html "access plus 600 seconds"

    ExpiresDefault "access plus 2 days"

</IfModule>

这很好。

我需要添加到这个 .htaccess:

  1. 从https://domain.pl/oferta(错误404 - 新网站中不存在页面)重定向到https://domain.pl/oferta-2020,https://domain.pl/kontakt 到https://domain.pl/contact

  2. 将其他错误 404 重定向到主页:https://domain.pl

我该怎么做?

【问题讨论】:

  • 我认为你可以通过 laravel 路线处理这个
  • 对我来说更好的是 .htaccess。有可能吗?
  • 我认为 laravel end 更好,因为你也可以添加逻辑,如果你想从 .htaccess 重定向,请查看help.dreamhost.com/hc/en-us/articles/…

标签: php laravel .htaccess laravel-5


【解决方案1】:

对于 404 页面,在 web.php

中添加此路由
Route::fallback(function () {
    return redirect('/');
});

【讨论】:

    【解决方案2】:

    在 web.php 中试试这个

    Route::get('oferta', function () {
        return redirect('oferta-2020');
    });
    Route::get('kontakt', function () {
        return redirect('contact');
    });
    Route::any('{all}', function () {
        return redirect('/');}
    )->where('all', '(.*)');
    

    在 laravel 端你可以重定向这个

    【讨论】:

    • 它不工作。我有旧网址:domena.pl/pl/godziny-plywania - 新网址是 domena.pl/plywalnia-godziny。我 maje 这段代码: Route::get('pl/godziny-plywania', function () { return redirect()->url('plywalnia-godziny'); }); - 这返回我错误方法 Illuminate\Routing\Redirector::url 不存在。
    • 我有旧网址:domena.pl/pl/zakupy - 在新网站中,此网址不存在。我在最后一行 web.php 中有你的代码: Route::get('{any}', function () { return redirect('/'); }); - 这不起作用。我有错误 404
    • 这个工作正常:Route::any('{all}', function () { return redirect('/'); })->where('all', '(.*) ');
    • 但是这个返回错误:Route::get('pl/godziny-plywania', function () { return redirect()->url('plywalnia-godziny'); }); - 方法 Illuminate\Routing\Redirector::url 不存在。
    • @opolopo 已修复,现在检查
    猜你喜欢
    • 1970-01-01
    • 2013-11-19
    • 2019-12-12
    • 2018-05-27
    • 2015-09-02
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    相关资源
    最近更新 更多