How can I use an .htaccess file in Nginx?

Nemanja

Premium Member
Joined
Nov 23, 2012
Messages
293
Points
28
I am currently using Nginx as my web server and I am in need of advice on how to redirect URLs without using an .htaccess file. As .htaccess is not supported by Nginx, I'm curious to know if there are any alternatives that I can use to achieve the same result. I am open to any suggestions or strategies from the community that have been effective in redirecting URLs in Nginx. Any help or guidance would be greatly appreciated!
 

alexmux

New member
Joined
Feb 17, 2022
Messages
14
Points
3
First of all, if you have a WP site, it's easy to do via plugins, you could choose the most handful one

But, in other case, you have to handle within a .conf file, which is in the document root directory of your site(s) typically, /etc/nginx/sites-available/directory_name.conf. The document root directory is where your site's files live, and it can sometimes be in the /html directory if you have one site on the server. Or if your server has multiple sites it can be at /domain.com. Either way that will be your .conf file name. In the /etc/nginx/sites-available/directory you'll find the default file that you can copy or use to append your redirects. Or you can create a new file name html.conf or domain.com.conf.
Some examples

server {
# Temporary redirect to an individual page
rewrite ^/oldpage$ domain/newpage redirect;
}

server {
# Permanent redirect to an individual page
rewrite ^/oldpage$ domain/newpage permanent;
}

server {
# Permanent redirect to non-www server_name www.domain.com;
rewrite ^/(.*)$ domain/$1 permanent;
}

server {
# Permanent redirect to www server_name domain.com;
rewrite ^/(.*)$ newdomain/$1 permanent;
}

server {
# Permanent redirect to new URL server_name olddomain.com;
rewrite ^/(.*)$ newdomain/$1 permanent;
}

server {
# Redirect to HTTPS listen 80;
server_name domain.com www.domain.com;
return 301 example_com$request_uri;
}

I hope it helps you
 

Hugo E.

Active member
Joined
Sep 8, 2014
Messages
288
Points
28
Best answer
Nginx does not support .htaccess files, which means that you cannot use the same syntax and directives in an .htaccess file in Nginx. However, you can achieve the same results by adding the necessary directives to your Nginx configuration file.

Here's how you can redirect URLs in Nginx:

- Create a server block for your domain in the Nginx configuration file. The location of this file depends on your distribution, but it's usually located in /etc/nginx/nginx.conf.

- Inside the server block, add a location block for the URL you want to redirect. For example, if you want to redirect http://example.com/foo to http://example.com/bar, you would add the following location block:
Code:
location /foo {
return 301 http://example.com/bar;
}
The return directive is used to return a 301 redirect status code along with the new URL.

- Save the Nginx configuration file and restart Nginx for the changes to take effect. The command to restart Nginx depends on your distribution, but it's usually one of the following:
Code:
sudo service nginx restart
sudo systemctl restart nginx
sudo /etc/init.d/nginx restart
Now, let's see how you can apply these steps in different control panels.

If you use CPanel

- Log in to your cPanel account and navigate to the "File Manager" section.
- Locate the public_html directory and find the .htaccess file you want to convert to Nginx.
- Right-click the .htaccess file and select "Edit".
- Copy the contents of the .htaccess file and paste them into a new file called nginx.conf. Save this file.
- Create a new file called .user.ini and add the following line to it:
Code:
cgi.fix_pathinfo=0
This disables the PHP feature called "path info" that can cause issues with Nginx.
- Log in to your server using SSH and navigate to the /etc/nginx/conf.d directory.
- Create a new file called example.com.conf (replace example.com with your domain name) and add the following contents:
Code:
server {
listen 80;
server_name example.com www.example.com;
root /home/username/public_html;

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

location /foo {
return 301 http://example.com/bar;
}

location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Replace "username" with your cPanel username.

- Save the file and restart Nginx using the command:
Code:
sudo service nginx restart
If you use Plesk
- Log in to your Plesk account and navigate to the "Domains" section.
- Click on the domain you want to configure.
- Click on the "Apache & Nginx Settings" tab.
- Scroll down to the "Additional Nginx directives" section and add the following:
Code:
location /foo {
return 301 http://example.com/bar;
}
Replace "example.com/bar" with the new URL you want to redirect to.

- Click on the "OK" button to save the changes.

If you use DirectAdmin

Log in to your DirectAdmin account and navigate to the "File Manager" section.
Locate the public_html directory and find the .htaccess file you want to
convert to Nginx.
- Right-click the .htaccess file and select "Edit".
- Copy the contents of the .htaccess file and paste them into a new file called nginx.conf. Save this file.
- Log in to your server using SSH and navigate to the /etc/nginx/conf.d directory.
- Create a new file called example.com.conf (replace example.com with your domain name) and add the following contents:
Code:
server {
listen 80;
server_name example.com www.example.com;
root /home/username/public_html;

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

location /foo {
return 301 http://example.com/bar;
}

location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Replace "username" with your DirectAdmin username.

- Save the file and restart Nginx using the command:
Code:
sudo service nginx restart
With these steps, you can redirect URLs in Nginx without using .htaccess files in cPanel, Plesk, and DirectAdmin. Remember to always test your changes and make backups before modifying your server configuration.
 

frankkieo

New member
Joined
Jun 26, 2023
Messages
1
Points
1
In the realm of Nginx, where .htaccess is absent, you can achieve URL redirection by modifying the Nginx configuration file located at "/etc/nginx/sites-available/". Utilize server or location block directives such as "rewrite" or "return" to redirect URLs with precision and control
 
Newer threads
Latest threads
Replies
1
Views
193
Replies
0
Views
202
Replies
0
Views
236
Replies
5
Views
555
Recommended threads

Referral contests

Referral link for :

Sponsors

Popular tags

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top