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]

Subscribe
Share

@mianofv

You may also want to take a look at this post, which has rewrites for NGINX. I don’t know for sure if they work with the latest version of OpenCart, though of all the posts I’ve looked at so far, they seem to be pretty consistent.

https://isenselabs.com/posts/how-to-install-and-run-opencart-on-nginx-server


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

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]

@mianofv

Since basic formatting tends to distort code, would you mind reposting those in a code block? You can do this by using the </> icon in the editor. Using a code block ensures proper formatting remains and we can then take a look at the rules.

Hi @mianofv

.htaccess does not exists in Nginx. That’s one of the reasons why Nginx is so fast, since nothing is loaded dynamically as Apache.

Can you use the code-button in this editor, because currently it removes underscores and other characters. Is the idea to load some elements over HTTP and not HTTPS? Why?