Question

.htaccess config to Nginx?

I am trying to change those Apache rewrite rules to Nginx, but auto converter fails. And I am not very good at them. Can anyone help please?

RewriteEngine On  RewriteBase /  RewriteCond %{HTTP:X-HTTPS} !1  RewriteCond %{REQUEST_URI} !^/exchange_1С_Opencart.php$  RewriteCond %{REQUEST_URI} !^/TEMP$  RewriteRule ^(.)$ https://%{HTTP_HOST}/$1 [R=301,L]  RewriteCond %{HTTP:X-HTTPS} 1 [NC]  RewriteCond %{REQUEST_URI} ^/exchange_1С_Opencart.php$  RewriteCond %{REQUEST_URI} ^/TEMP$  RewriteRule ^(.)$ http://%{HTTP_HOST}/$1 [R=301,L]    2 variant

RewriteEngine On RewriteBase / RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} !^/exchange_1С_Opencart.php$ [NC] RewriteCond %{REQUEST_URI} !^/TEMP$ [NC] RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,QSA] RewriteCond %{SERVER_PORT} 443 RewriteRule ^(/exchange_1С_Opencart.php)$ http://%{HTTP_HOST}/$1 [R=301,L,QSA] RewriteRule ^(/TEMP)$ http://%{HTTP_HOST}/$1 [R=301,L,QSA]

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

@mianofv Wow, that’s a difficult config to read - specially if you’re new to Nginx, so I understand why you can’t figure it out.

Instead of trying to hack around with http vs https, maybe we should simply look at exchange_1С_Opencart.php file and see why it doesn’t work with https (because it should work without any problems, since you’re already redirecting http to https). Can you contact the developer to get it fixed?

Otherwise, you need to not use the CentMinMod wizard anymore, since it might overwrite some of the changes you’re going to make. It’s a little difficult to figure out the configuration, since there’s so many includes. This might work - but it might “explode”, so make a copy of domain.com.ssl.conf before replacing with this. Remember to replace all the domain.com with the correct domain.

server {
  server_name domain.com www.domain.com;
  root /home/nginx/domains/domain.com/public;

  access_log /home/nginx/domains/domain.com/log/access.log combined buffer=256k flush=60m;
  error_log /home/nginx/domains/domain.com/log/error.log;

  location = /exchange_1С_Opencart.php {
    include /usr/local/nginx/conf/php.conf;
    break;
  }
  location = /TEMP {
    include /usr/local/nginx/conf/staticfiles.conf;
    break;
  }

  location / {
    return 302 https://$server_name/$request_uri;
  }
}

server {
  listen 443 ssl http2;
  server_name domain.com www.domain.com;

  root /home/nginx/domains/domain.com/public;

  access_log /home/nginx/domains/domain.com/log/access.log combined buffer=256k flush=60m;
  error_log /home/nginx/domains/domain.com/log/error.log;

  ssl_dhparam /usr/local/nginx/conf/ssl/domain.com/dhparam.pem;
  ssl_certificate      /usr/local/nginx/conf/ssl/domain.com/domain.com-acme.cer;
  ssl_certificate_key  /usr/local/nginx/conf/ssl/domain.com/domain.com-acme.key;
  include /usr/local/nginx/conf/ssl_include.conf;
  http2_max_field_size 16k;
  http2_max_header_size 32k;
  ssl_ciphers     EECDH+CHACHA20-draft:EECDH+CHACHA20:EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA+SHA256:EECDH+ECDSA+SHA384:EECDH+aRSA+SHA256:EECDH+aRSA+SHA384:EECDH+AES128:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS:!RC4:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!CAMELLIA;
  ssl_prefer_server_ciphers   on;
  ssl_buffer_size 1369;
  ssl_session_tickets on;
  resolver 8.8.8.8 8.8.4.4 valid=10m;
  resolver_timeout 10s;
  ssl_stapling on;
  ssl_stapling_verify on;
  ssl_trusted_certificate /usr/local/nginx/conf/ssl/domain.com/domain.com-acme.cer;

  location = /exchange_1С_Opencart.php {
    return 302 http://$server_name/$request_uri;
  }
  location = /TEMP {
    return 302 http://$server_name/$request_uri;
  }

  location ~* \.(ttf|ttc|otf|eot|woff|woff2|font.css|css)$ {
    add_header Access-Control-Allow-Origin *;
  }

  include /usr/local/nginx/conf/staticfiles.conf;
  include /usr/local/nginx/conf/php.conf;
  include /usr/local/nginx/conf/drop.conf;
  include /usr/local/nginx/conf/vts_server.conf;
}

Are you’ve change the configuration, then you need to test the config and restart Nginx:

sudo nginx -s configtest
sudo nginx -s restart

Thanks! Module is connecting. But not uploading the files… I guess only a developer of that module can fix that.

Thank you for your answers. I have a program installed on my computer in my shop which synchronizes products to my OpenCart . My OpenCart is with HTTPS, but that OpenCart module works only with HTTP. So I need to force those files to redirect to HTTP only. The developer only gave Apache rules.

RewriteEngine On 
RewriteBase / 
RewriteCond %{HTTP:X-HTTPS} !1 
RewriteCond %{REQUESTURI} !^/exchange1СOpencart.php$ 
RewriteCond %{REQUESTURI} !^/TEMP$ 
RewriteRule ^(.)$ https://%{HTTPHOST}/$1 [R=301,L] 
RewriteCond %{HTTP:X-HTTPS} 1 [NC] 
RewriteCond %{REQUESTURI} ^/exchange1СOpencart.php$ 
RewriteCond %{REQUEST_URI} ^/TEMP$ 
RewriteRule ^(.)$ http://%{HTTP_HOST}/$1 [R=301,L] 
RewriteEngine On 
RewriteBase / 
RewriteCond %{SERVERPORT} 80 
RewriteCond %{REQUESTURI} !^/exchange1СOpencart.php$ [NC] 
RewriteCond %{REQUESTURI} !^/TEMP$ [NC] 
RewriteRule ^(.*)$ https://%{HTTPHOST}/$1 [R=301,L,QSA] 
RewriteCond %{SERVERPORT} 443 
RewriteRule ^(/exchange1СOpencart.php)$ http://%{HTTPHOST}/$1 [R=301,L,QSA]
RewriteRule ^(/TEMP)$ http://%{HTTP_HOST}/$1 [R=301,L,QSA]

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.