1

I have been trying to rewrite my php pages so that they display without their extensions in the browser. For example, a link on my server at www.example.com loads a page called about-us.php and in the browser, the user would see www.example.com/about-us.

I have been trying to follow these tutorials to try and get this to work, but sadly they are not working.

Tweak Talk Link

Stack Overflow Link

Here is my nginx sites-available/default file content.

# Server Block
server {
    # Listening Ports
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    # Root Location Initialization
    root /home/zach/Documents/Workspaces/www;
    index index.php index.html index.htm;

    # Server Name
    server_name localhost;

    # Root Location
    location / {
        try_files $uri $uri/ @extensionless-php;
    }

    # Error pages
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /home/zach/Documents/Workspaces/www;
    }

    # PHP Handling
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

All of my links to php pages still display the .php extension in the browser, does anyone have any solutions to my problem?

4
  • This question might be better suited for Server Fault Commented Apr 29, 2015 at 3:24
  • 1
    @AlphaDelta this question is about nginx - it directly involves tools used primarily for programming Commented Apr 29, 2015 at 3:26
  • Definitely Server Fault. Commented Apr 29, 2015 at 3:27
  • @scrowler exactly this is a question about nginx, a server software, it is not a programming or scripting problem. Commented Apr 29, 2015 at 3:41

1 Answer 1

0

Add the following line to the exact spot where you think it'd form a loop! :-)

if ($request_uri ~ ^/([^?]*)\.php($|\?)) {  return 302 /$1?$args;  }

More info here: nginx remove .php and .html file extension

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.