0

I have next config in htaccess

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

it works well if I type example.com or http://example.com it redirect correctly to https://example.com, but if I type example.com/section it redirect to https://example.com/index.php which is not correct, how to fix this?

Adding the "/$1**" after index.php on this part of htaccess file then we get https://example.com/index.php/subroute, which redirects correctly to the sub route BUT STIL SHOWING INDEX.PHP in the URL

# Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php**/$1** [L]
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
JuulFan
  • 21
  • 7
  • Does `RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]` work? – user3783243 Jun 13 '20 at 10:52
  • No, it redirect to index.php again, i have to mention im using laravel framework wich make routes and not files, maybe if this is the conflict ? – JuulFan Jun 13 '20 at 10:59
  • This indicates this approach should work. https://stackoverflow.com/questions/28402726/laravel-5-redirect-to-https Can you replace to `https://example.com` just to confirm the rules are being matched? Sometimes browser will cached redirect and wont even look at new rule. – user3783243 Jun 13 '20 at 11:04
  • "Can you replace to https://example.com" Where exactly ? And thinking about it i think is not problem with using a framework, cause the problem happens before. somethin in htaccess file is redirecting incorrectly. – JuulFan Jun 13 '20 at 11:19
  • Change `https://%{HTTP_HOST}%{REQUEST_URI}` to a different host altogether to verify the redirect changes are firing. – user3783243 Jun 13 '20 at 11:27
  • i did, not working, in htaccess file, there a section where redirect if not folder i think this the problem, because routes in laravel are not folder, so dont contain index.php, but im not sure how to fix it. – JuulFan Jun 13 '20 at 12:03
  • Is `RewriteEngine On` not the first line? I would expect something like `RewriteCond %{REQUEST_FILENAME} !-f` later in the file but this should function first and the `L` should not make the other execute. – user3783243 Jun 13 '20 at 12:08

2 Answers2

0

i fixed my issue by using bellow code you can Try this

<IfModule mod_rewrite.c>
    RewriteEngine On 
    RewriteCond %{HTTPS} off [OR] 
    RewriteCond %{HTTP_HOST} !^www\. [OR] 
    RewriteCond %{HTTP_HOST} ^example\.com [NC] 
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE] 
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/  
    RewriteRule ^index\.php(/(.*))?$ example.com/$2 [R=301,L] 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule>
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Not working. same happens just work when type the main url http:// example.com or example.com, but when you go to a sub route like example.com/subroute it redirects to https:// example.com/index.php instead https:// example.com/subroute – JuulFan Jun 13 '20 at 10:49
  • This isn't considering SSL connections. – user3783243 Jun 13 '20 at 10:50
  • Yes @user3783243 you're right, anyway still not working – JuulFan Jun 13 '20 at 11:01
0

This is the code I use to redirect to HTTPS for Laravel:

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

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

    # Force     SSL redirect:
    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://example.com/$1 [R,L]
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.example.com/$1 [R,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]
</IfModule>
Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
  • Not working. same happens just work when type the main url http :// example.com or example.com, but when you go to a sub route like http://example.com/subroute it redirects to https :// example.com/index.php instead https :// example.com/subroute – JuulFan Jun 13 '20 at 10:45
  • @JuulFan mmm what server are you running on? this works fine in APACHE, your server probably is not getting the htaccess, try rebooting it – Alberto Sinigaglia Jun 13 '20 at 10:46
  • Apache, im using laravel framework i dont know if this the problem. – JuulFan Jun 13 '20 at 11:01
  • @JuulFan here you are the entire htaccess i'm using for my laravel project, remember to enable rewrite engine (check this https://stackoverflow.com/questions/43509974/laravel-rewrite-not-working/43510059) – Alberto Sinigaglia Jun 13 '20 at 11:32
  • I dont know what happen, but now it WORKS! thank you ! – JuulFan Jun 13 '20 at 14:34