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 !!