In this tutorial, we are learning how to rewrite module in nginx and centOs machine. We are going to see how to do it for the mail directory and also for the sub-directory of the project.
Let’s see it
Nginx Mod_Rewrite URL Example
I am assuming you have CentOS machine and Nginx is installed on it. There are following steps will follow to implement url rewrite rules –
Step 1: I am assuming you’re using the default.conf
file.We will open this file which is stored in /etc/nginx/conf.d/
folder –
Step 2: Search the below code into the virtual host file(default.conf).
1
2
3
4
|
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
|
We will make following changes into the above line and save the file –
1
2
3
4
5
|
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
|
Step 3: We will reload Nginx server service using below command –
sudo service nginx reload
How To Enable Mod_Rewrite for Subfolder
Normally, web hosting provider install WordPress site into the sub folder(sub_folder_name) then , We need to change location block as like below –
from –
1
2
3
4
|
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
|
replace To –
1
2
3
|
location /sub_folder_name/ {
try_files $uri $uri/ /sub_folder_name/index.php?$args;
}
|
You need to restart nginx service or server. sudo service nginx reload
Thank you so much. Hope this was helpful for you.
Happy Learning !!