By anthropophagus

2014-02-20 14:51:20 8 Comments

I want my nginx make display all url's clean.

With some research I've made the first case to work. It`s done by following configuration:

location / {
    root   html;
    index  index.html index.htm index.php;
    try_files $uri.html $uri/ =404; 

It works for indexhtml.html displaying as indexhtml, but nothing happens with .php. If I change $uri.html to $uri.php, it works neither for .html, neither .php. I`ve tried to put something similar in php location but without any success.

Any advices?


@Łukasz Pniewski 2017-01-12 21:27:56

Perhaps this may be of use for you... It' Simple and gets the job done:

location / {
  rewrite ^/([^\.]+)$ /$1.html break;

@costamatrix 2019-05-11 03:19:39

it worked, but now php is not being processed...the php was downloaded!

@Mohammad AbuShady 2014-02-20 17:57:17

No need for extra blocks and named locations and everything. Also move the index line outside the location block

server {
  index index.html index.php;
  location / {
    try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
  location ~ \.php$ {
    try_files $uri =404;
    # add fastcgi_pass line here, depending if you use socket or port

Keep in mind that if you have a folder and a file with the same name inside the same folder, like /folder/xyz/ and /folder/xyz.php you won't be able to run the php file if the folder xyz contains an index.php or index.html, just keep this in mind.

@Mohammad AbuShady 2014-08-22 00:45:39

@elbowlobstercowstand, just another addition, when using $query_string, I prefer using $is_args instead of a plain ? would be a tiny bit cleaner when there's no actual args by not having a trailing ? in the url.

@William Turrell 2016-04-12 15:03:00

Just to add, the important bit in making this work is the =404 needs to be in the try_files in the .php block, not at the end of the location / one, otherwise you'll get 404 errors for URLs for which a .php file does exists.

@Basil Musa 2017-04-06 12:27:20

@elbowlobstercowstand was right, I rolled back his last edit, nginx will not see the _GET array if ?$query_string was not added.

@cnst 2015-10-06 09:17:20

To further Mohammad's answer, you might also want to offer redirects from .html and .php to the extensionless versions.

This can be accomplished due to the fact that $request_uri contains "full original request URI (with arguments)", and is not affected by the internal rewrites that are not visible to the user.

server {
  index index.html index.php;
  location / {
    if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
    try_files $uri $uri/ $uri.html $uri.php?$args;
  location ~ \.php$ {
    if ($request_uri ~ ^/([^?]*)\.php($|\?)) {  return 302 /$1?$args;  }
    try_files $uri =404;
    # add fastcgi_pass line here, depending if you use socket or port

@Tag 2015-10-19 21:29:55

"Directive if has problems when used in location context, in some cases it doesn’t do what you expect but something completely different instead. In some cases it even segfaults. It’s generally a good idea to avoid it if possible."

@cnst 2015-10-19 21:33:47

@Tag, sounds like there should be a separate page ifisevilisevil; it you actually read the ifisevil document you've linked, you'll note that doing a return from within if is explicitly advertised as always being 100% safe, and that's exactly what i'm doing in my answer

@Tag 2015-10-19 21:41:05

It may be safe, but it isn't the best practice. Please read Pitfalls and Common Mistakes.

@cnst 2015-10-19 22:16:36

@Tag, since when is using a safe if is not the best practice? Also, why are you linking to a page that doesn't even claim that it's not the best practice, yet act as if it does claim that it's not?

@dzhi 2016-08-02 09:36:53

As an SEO Expert, I say this should be the accepted answer.

@Eugene Zakharenko 2017-05-18 09:08:53

It can be done without IF:…

@Jack 2014-02-20 15:04:07

From what I've researched, if you append your /etc/nginx/conf.d/domain.tld.conf file to include:

location / {
    try_files $uri $uri.html $uri/ @extensionless-php;
    index index.html index.htm index.php;

location ~ \.php$ {
    try_files $uri =404;

location @extensionless-php {
    rewrite ^(.*)$ $1.php last;

Then restart nginx and give it a go. Hopefully this will help you! More information can be found (where I found it) here @

@anthropophagus 2014-02-20 15:14:49

Funny. If I delete codetry_files $uri.html $uri/ =404;code from my previous configuration and add Yours, than everything works fine with php, but doesn`t work with html. If I do not remove previous line and add Yours, "Problem loading page" shows up. How can I combine them to work both?

@Jack 2014-02-20 15:19:51

Try changing your 'try_files' line under 'location' to: try_files $uri $uri.html $uri/ @extensionless-php; Or possibly: try_files $uri $uri.html $uri/ @extensionless-php $uri/ =404;

@anthropophagus 2014-02-20 15:29:31

Thanks a lot! I replaced try_files $uri $uri/ @extensionless-php; with try_files $uri.html $uri/ @extensionless-php; and it`s finally working both for html and php. Great!

@Tom Hagen 2015-07-25 12:28:56

Incase you have installed nginx on Windows n cant seem to find "/etc/nginx/conf.d/domain.tld", then this is actually the same as "nginx/conf/nginx.conf"

Related Questions

Sponsored Content

37 Answered Questions

[SOLVED] Deleting an element from an array in PHP

  • 2008-12-15 20:28:55
  • Ben
  • 2344938 View
  • 2297 Score
  • 37 Answer
  • Tags:   php arrays unset

28 Answered Questions

[SOLVED] Get the full URL in PHP

  • 2011-07-20 21:29:51
  • DiegoP.
  • 2622355 View
  • 978 Score
  • 28 Answer
  • Tags:   php url

31 Answered Questions

[SOLVED] How to create an HTML button that acts like a link?

32 Answered Questions

[SOLVED] How do I get a YouTube video thumbnail from the YouTube API?

28 Answered Questions

[SOLVED] How can I prevent SQL injection in PHP?

7 Answered Questions

[SOLVED] How does PHP 'foreach' actually work?

30 Answered Questions

[SOLVED] Convert HTML + CSS to PDF with PHP?

30 Answered Questions

[SOLVED] How do you parse and process HTML/XML in PHP?

26 Answered Questions

[SOLVED] Redirect from an HTML page

18 Answered Questions

[SOLVED] How do I modify the URL without reloading the page?

Sponsored Content