-4

How to redirect from :

  • https://www.example.com
  • http://www.example.com
  • http://example.com

to :

  • https://example.com

How can I do this with .htaccess file?

Braiam
  • 1
  • 11
  • 47
  • 78

3 Answers3

1

you can do that by add this code to .htacces file

RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

note if you want remove www from url

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
menasoft
  • 135
  • 1
  • 2
  • 12
0

Define a middleware for removing www from the url using below command php artisan make:middleware RedirectToNonWwwMiddleware and write below code in handle method of that middleware

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);
    }

Add the middleware to the array of $middleware inside app\Http\Kernel.php file like below

protected $middleware = [

    // Other middlewares ...

    \App\Http\Middleware\RedirectToNonWwwMiddleware::class,
];

Define the url inside your .env file with https like below

APP_URL=https://example.com

At last clear the config cache by running php artisan config:clear.

SEYED BABAK ASHRAFI
  • 4,093
  • 4
  • 22
  • 32
-2

I found the best answer to my question :

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

It supported all of positions that I said.

You can test it in : Test .htaccess

so you can redirect from :

  • https://www.example.com
  • http://www.example.com
  • http://example.com

to :

  • https://example.com

with that code.

  • This will create a "redirect-loop" since the second rule redirects to itself. (The MWL htaccess tester is not able to catch this.) @menasoft had already posted an acceptable solution and you seem to have just copied it and reversed the two rules - which makes no difference whatsoever. (?) – MrWhite Mar 23 '21 at 12:28
  • it is the best solution to do it. I tested it. – Mahdi Sadeghi Mar 24 '21 at 09:09
  • How did you "test" it? The directives you posted are obviously wrong - they can not possibly work as written. As noted in my comment, the directives you've posted will result in an endless redirect-loop (if they are executed at all), because the second rule redirects the canonical hostname (ie. `example.com` - no www) to itself. If, however, HTTP and HTTPS resolve to different hosts and these directives are on the HTTP host then there will be no "loop", but then requests to the non-canonical hostname on HTTPS won't be canonicalised. (?!) – MrWhite Mar 24 '21 at 13:22
  • Even using the MWL `.htaccess` tester that you referenced you can still deduce the error... when you request `https://example.com/foo` you can see that it will generate a "redirect loop" because the "output URL" is the same as the "input URL".... https://htaccess.madewithlove.be?share=4c6d6cd0-fb9d-4d18-9f86-c3484e8f455d / When you request `https://example.com/foo` (ie. the canonical scheme + hostname) there should be "no" output - it should not do anything. – MrWhite Mar 24 '21 at 13:32
  • I tested it again it worked perfect.when you put https:// example. com the output is https:// example. com too so it working perfect – Mahdi Sadeghi Mar 25 '21 at 21:27
  • When you say, "tested", do you mean in that testing tool, or on a real server? If the output (from that "testing tool") is the same as the input then that is an error - it will result in an endless redirect loop. When you request the canonical URL there should be "no" output at all - it should not do anything. – MrWhite Mar 25 '21 at 22:27
  • I tested it in testing tool and real server. In both of them result was perfect – Mahdi Sadeghi Mar 26 '21 at 05:57
  • Testing tool is great. It can show you the output of the code and debugging it. – Mahdi Sadeghi Mar 26 '21 at 06:25
  • That "testing tool" has a lot of bugs. (But you are misinterpreting the results of that tool in this case.) There's no way the code you posted can work on a "real" server. – MrWhite Mar 26 '21 at 11:06
  • I said I tested it in real server and it worked perfectttttttt – Mahdi Sadeghi Mar 26 '21 at 19:32