php - HTTP to HTTPS Redirection Issue

840

I want to redirect http requests to https node.

I tried,

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Please find below few test results, I executed,

http://sample.mydomane.co/ redirected to https://sample.mydomane.co/

// Worked as expected

http://sample.mydomane.co/api-docs/ not redirected to https://sample.mydomane.co/api-docs/

//Didn't work as expected

EDIT: This is my.htaccess file

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

    #Options +FollowSymLinks
    RewriteEngine On

    RewriteRule ^(api-docs|docs)($|/) - [L] # HERE IS THE PROBLEM I THINK

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

    # redirect all http -> https
    RewriteCond %{HTTPS} !on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

</IfModule>

Please let me know how to achieve or fix this.

49

Answer

Solution:

I use with success the following rule, note the difference of the regex pattern

RewriteRule ^(.*)$ https://domainname/$1 [R,L]
499

Answer

Solution:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]

Apache Wiki

http://wiki.apache.org/httpd/RewriteHTTPToHTTPS

People are also looking for solutions to the problem: linux - No stdout when php process is spawned in the background

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.