如何将站点 URL 重定向到 https://example.com
How to redirect site URLs to https://example.com
如何重定向自:
https://www.example.com
http://www.example.com
http://example.com
至:
https://example.com
如何使用 .htaccess
文件执行此操作?
您可以通过将此代码添加到 .htaccess 文件来做到这一点
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/ [L,R=301]
如果您想从 url
中删除 www,请注意
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/ [L,R=301]
使用下面的命令 php artisan make:middleware RedirectToNonWwwMiddleware
定义一个用于从 url 中删除 www 的中间件,并在该中间件的 handle 方法中编写下面的代码
public function handle($request, Closure $next)
{
if (substr($request->header('host'), 0, 4) == 'www.') {
$request->headers->set('host', config('app.url'));
$trailing = $request->path() == '/' ? '' : '/';
return \Redirect::to(config('app.url') . $trailing . $request->path());
}
return $next($request);
}
将中间件添加到 app\Http\Kernel.php
文件中 $middleware
的数组中,如下所示
protected $middleware = [
// Other middlewares ...
\App\Http\Middleware\RedirectToNonWwwMiddleware::class,
];
使用 https 在 .env 文件中定义 url,如下所示
APP_URL=https://example.com
最后通过运行php artisan config:clear
清除配置缓存。
我找到了我的问题的最佳答案:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://example.com/ [L,R=301]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/ [L,R=301]
支持我说的所有立场
您可以在以下位置进行测试:
Test .htaccess
因此您可以从 :
重定向
https://www.example.com
http://www.example.com
http://example.com
至:
https://example.com
使用该代码。
如何重定向自:
https://www.example.com
http://www.example.com
http://example.com
至:
https://example.com
如何使用 .htaccess
文件执行此操作?
您可以通过将此代码添加到 .htaccess 文件来做到这一点
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/ [L,R=301]
如果您想从 url
中删除 www,请注意RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/ [L,R=301]
使用下面的命令 php artisan make:middleware RedirectToNonWwwMiddleware
定义一个用于从 url 中删除 www 的中间件,并在该中间件的 handle 方法中编写下面的代码
public function handle($request, Closure $next)
{
if (substr($request->header('host'), 0, 4) == 'www.') {
$request->headers->set('host', config('app.url'));
$trailing = $request->path() == '/' ? '' : '/';
return \Redirect::to(config('app.url') . $trailing . $request->path());
}
return $next($request);
}
将中间件添加到 app\Http\Kernel.php
文件中 $middleware
的数组中,如下所示
protected $middleware = [
// Other middlewares ...
\App\Http\Middleware\RedirectToNonWwwMiddleware::class,
];
使用 https 在 .env 文件中定义 url,如下所示
APP_URL=https://example.com
最后通过运行php artisan config:clear
清除配置缓存。
我找到了我的问题的最佳答案:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://example.com/ [L,R=301]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/ [L,R=301]
支持我说的所有立场
您可以在以下位置进行测试: Test .htaccess
因此您可以从 :
重定向https://www.example.com
http://www.example.com
http://example.com
至:
https://example.com
使用该代码。