2

I'm trying to become my php files running without .php extension ending in browser and redirect all to non .php

I've already setup, that both available. With .php and without .php on browser. But now just need a rewrite.

Did u can help me out their?

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

Best regards

2
  • Have you try this answer stackoverflow.com/a/21911610/8828489? Commented Dec 17, 2018 at 0:14
  • Ist just the same without this: location / { index index.html index.htm index.php; } location ~ \.php$ { try_files $uri =404; } - where index index.html index.htm index.php; already defined in root directory and dont think try_files is for an rewrite ^^ On my side just the rewrite from .php to without is missing. Commented Dec 17, 2018 at 0:22

1 Answer 1

1

You presumably have a location ~ \.php block to process URIs which end with .php. And you should include a try_files $uri =404; statement in that block to avoid passing uncontrolled requests to PHP.

You can also add an internal directive to that block, which will prevent external access to the .php URI, so that only your extensionless PHP URIs will work. See this document for details.

For example:

location ~ \.php$ ( internal; try_files $uri =404; ... } 

If you want to redirect the browser to use extensionless PHP when a URI ending with .php is inadvertently presented, you need to look at the original request URI, otherwise you risk a redirection loop.

For example:

if ($request_uri ~ \.php($|\?)) { rewrite ^(.*)\.php$ $1 permanent; } 

See this caution on the use of if.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.