1 Replies - 765 Views - Last Post: 10 August 2015 - 09:57 AM

#1 huzi8t9   User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 373
  • Joined: 11-July 07

NGINX - URL Rewriting

Posted 10 August 2015 - 07:49 AM

Good afternoon.

I'm working with a client; they're using NGINX and would like to neaten up their URLs, so I agreed that I would sort it out for them.

I (wrongly) thought it would be as simple as creating a .htaccess file - how naive I was! I've got SSH access and I keep following different tutorials online of how to implement URL rewriting... however, when I put in my line of code: location / { rewrite ^([0-9]+)/?$ user.php?id=$1 last; }. I thought it looked good, went to restart NGINX and received the following error: Restarting nginx (via systemctl): Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details. [FAILED]

The .conf file I opened is located at /etc/nginx/nginx.conf and my main directory is /home/domain.com/public_html

This is my first ever contact with NGINX so, please, be nice!

Thanks in advanced


Is This A Good Question/Topic? 0
  • +

Replies To: NGINX - URL Rewriting

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6727
  • View blog
  • Posts: 31,157
  • Joined: 10-May 07

Re: NGINX - URL Rewriting

Posted 10 August 2015 - 09:57 AM

When I've done URL cleanup under nginx, I still used the .htaccess file to handle the task rather than the webserver.

Default with 404, serve anything else :
nginx :
        root /var/www/html/site/;
        location / {
           try_files $uri $uri/ $uri.html index.html = 404;

Then strip file extension (in this case, html) & serve like wordpress style :
.htaccess :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]

** As a side note **
while reviewing my nginx configs, the only time I handled url re-write within the nginx config was for subdomains such as :

http://example.site.com -> http://realsite.com

This would be a situation where I gave a customer only view for their project, & then once completed I forwarded from the preview server to the production server. So my URL rewrites within nginx config files are only at the domain level. For handling the URL path / arguments, I still handle with .htaccess.

Hopefully this is helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1