Panel
PowerQA is a state of the art discussion software that makes online discussions fun. It's simple, fast, and powerful.
Newbie
yerbol89
0 votes
1,046 views

How i add auto redirect http for https

Hi all. How i auto redirect my site http for https.

Today i add my site https protocol. https://site.com

Site open adress https://site.com and http://site.com How i auto redirect my site url http://site.com for https://site.com

My site htaccess file.

DirectoryIndex index.php 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} robots.txt$ [NC] 
RewriteRule ^([^/]+) $1 [L] 
RewriteCond %{HTTP_HOST} ^www.site\.com 
RewriteRule ^(.*)$ http://site.com/$1 [R=301,L] 
#RewriteBase / 
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ 
RewriteRule . %1/%2 [R=301,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L] 
</IfModule>

i add after RewriteEngine On code

RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

But this code dont work.

by yerbol89 asked Aug 29, 2015 in Core

Please log in or register to add a comment.

Please log in or register to answer this question.

2 Answers

Expert
Vanbells
+2 votes
Put the below code in 
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
 
Or add the following code 

RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?your domain name\.com$ [NC]
RewriteRule ^$ https://www.your domain name.com/%{REQUEST_URI} [R,L]

The above code is good but not the best.
Apache docs does not recommend using rewrite to direct users.

use this instead

To redirect http URLs to https, do the following:

<VirtualHost *:80>
    ServerName www.example.com
    Redirect / https://www.example.com/
</VirtualHost >

<VirtualHost *:443>
    ServerName www.example.com
    # ... SSL configuration goes here
</VirtualHost >
by Vanbells answered Aug 29, 2015
by Vanbells edited Aug 29, 2015

1 Comment

I found my problem. I use my site Nginx+Apache and this you code dont work my server.

When using Nginx + Apache bundles will result in an error of the cyclic forward. Therefore, the redirect will need to configure it in Nginx:
http://www.powerqa.org/qa/136/how-i-add-auto-redirect-http-for-https?show=138#a138

Please log in or register to add a comment.

Newbie
yerbol89
0 votes

301 redirect from http to https

Nginx you need to create two server section in the configuration file, one for http (port 80), one for https (port 443).
For the new domain conf. file nginx
Section server, to open at http (port 80) and forwarding settings:

server {
    listen IP server: 80;
    server_name www.domain.com;
    rewrite ^ https: //www.domain.com$request_uri? permanent;
}

Section server, to open at https (port 443), where the main domain settings.

server {
    listen IP server: 443;
    server_name www.domain.com;
.....
}

For existing domain Conf. File nginx
If you make changes to an existing section of the conf. File nginx do this: From the main section of the domain, remove the string of the form

listen IP server: 80;

And create a new server section like this:

server {
    listen IP server: 80;
    server_name www.domain.com;
    rewrite ^ https: //www.domain.com$request_uri? permanent;
}

After making changes to the configuration file Nginx, for entry into force of these changes, you need to restart it like this:

service nginx restart

The trick is that I can not do it. :)

My ngnix.conf file:

user www-data;
worker_processes auto;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    # multi_accept on;
}

http {
    include       /etc/nginx/mime.types;

    access_log    /var/log/nginx/access.log;

    sendfile        on;
    tcp_nopush      on;

    keepalive_timeout  30;
    keepalive_requests 400;
    reset_timedout_connection on;
    tcp_nodelay        on;
    server_tokens off;

    ssl_prefer_server_ciphers On;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
    
    open_file_cache max=200000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;

    client_max_body_size 8m;
    client_body_buffer_size 4m;

    proxy_connect_timeout 300;
    proxy_send_timeout 300;
    proxy_read_timeout 300;
    proxy_buffer_size 64k;
    proxy_buffers 8 256k;
    proxy_busy_buffers_size 256k;
    proxy_temp_file_write_size 10m;

    gzip  on;
    gzip_comp_level     8; 
    gzip_vary on;
    gzip_static off;
    gzip_types text/css text/plain application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;
    gzip_min_length  1024;
    gzip_disable     "msie6";
    gzip_proxied     any;

    include /etc/nginx/conf.d/default_server.conf;

    server_names_hash_bucket_size 128;
    log_format isp '$bytes_sent $request_length';
    include /usr/local/ispmgr/etc/nginx.domain;
    server {
        server_name szh.kz www.szh.kz;
        listen 185.22.232.116;
        listen 185.22.232.116:443 ssl;
        disable_symlinks if_not_owner from=$root_path/$subdomain;
        set $root_path /var/www/szh/data/www/szh.kz;
        set $subdomain "";
        location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
            access_log off;
            access_log /var/www/nginx-logs/szh isp;
            expires 30d;
            root $root_path/$subdomain;
            error_page 404 = @fallback;
        }
        location / {
            proxy_pass http://185.22.232.116:81;
            proxy_redirect http://185.22.232.116:81/ /;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Real-IP $remote_addr;
        }
        location @fallback {
            proxy_pass http://185.22.232.116:81;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Real-IP $remote_addr;
        }
        include /usr/local/ispmgr/etc/nginx.inc;
        if ($host ~* ^((.*).szh.kz)$) {
            set $subdomain ../$1;
        }
        index index.php;
        ssl_certificate /var/www/httpd-cert/szh/szh.kz.chained.crt;
        ssl_certificate_key /var/www/httpd-cert/szh/szh.kz.key;
    }
}

by yerbol89 answered Aug 30, 2015

1 Comment

so  is it finally fixed? or you haven't still fixed it yet?

Please log in or register to add a comment.

Welcome to PowerQA. PowerQA is a new discussion software (not OSS). Here is a community for PowerQA developers. Current stable version is V1.9.4. It is enhanced day by day.

Statistics

  • 338 questions
  • 276 answers
  • 480 comments
  • 100 users

Post types

...