Cara setting htaccess di nginx

Bismillaahirrohmaanirrohiim…

Bicara tentang htaccess, sangat berkaitan erat dengan web server apache.
Kita tentu sudah tahu bahwa file htaccess tidak dapat dijalankan di nginx.
Padahal banyak web atau blog yang menggunakan htaccess untuk rewrite url, seperti wordpress, dll.

Nah sekarang bagaimana agar isi dari htaccess tersebut bisa dijalankan di nginx.

1. Pertama buka konfigurasi nginx

/etc/nginx/nginx.conf

2. Edit file konfigurasi nginx sesuai kebutuhan
Ketika edit nginx.conf, cari kode blok :

server {
    #isi konfigurasi server
    #edit file disini
    #isikonfigurasi server
}

Bisa jadi edit konfigurasi dengan cara membuat kode blok server {} baru.
Penjelasan editnya ada di bawah.

3. Restart nginx

systemctl restart nginx

Selesai

Edit file konfigurasi nginx

Rewrite url ke file index

Maksudnya, jika membuka url apapun yang tidak menuju file langsung, diredirect ke index.php

location / {
    try_files $uri $uri/ /index.php?$args;
}

Jika web berada pada subfolder:

location /wordpress {
    try_files $uri $uri/ /wordpress/index.php?$args;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(/wordpress)(/.*)$;
}
Untuk disable logging file favicon, robots, dll
location = /favicon.ico {
        log_not_found off;
        access_log off;
}
location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
}
Untuk redirect dari www menjadi non www
  server {
      server_name www.domain.com;
  listen IPSERVER;
  root /home/domain/public_html;
  index index.html index.htm index.php;
      return 301 http://domain.com$request_uri;
  }
server {
  server_name domain.com;
  listen IPSERVER;
  root /home/udomain/public_html;
  index index.html index.htm index.php;
      dst...
  }

demikian juga sebaliknya dari non www menjadi www, tinggal dibalik server_name www.domain.com; menjadi server_name domain.com; dan seterusnya.

Baca Juga:   Cara install netspeedmonitor di windows 10 sekaligus settingnya

sumber lain mengatakan bisa juga dengan:
rewrite ^(.*) http://domain.com$1 permanent;
untuk menggantikan
return 301 http://domain.com$request_uri;

Untuk redirect dari http menjadi https
server {
  listen 80 default_server;
  listen [::]:80 default_server;
  server_name domain.com www.domain.com;
  return 301 https://$server_name$request_uri;
}

atau jika untuk spesifik ke domain tertentu:

server {
  listen 80;
  listen [::]:80;
  server_name domain.com;
  return 301 https://domain.com$request_uri;
}
Untuk control expires header file statis
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
}

Demikian cara setting htaccess di nginx, silahkan dicoba.
Note: Jangan gunakan tab saat edit file konfigurasi ini, tapi gunakan spasi.

Semoga bermanfaat.

Sebagian Sumber:
https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/
https://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www

Bagikan

You May Also Like

About the Author: rasupe

Leave a Reply

Your email address will not be published. Required fields are marked *