Page 1 of 1

Remove .PHP extension in NGINX?

Posted: Sat Oct 07, 2017 3:19 pm
by Nou4r
How can i remove the .php extensions from my urls?
For example:

www.domain.com/page.php
to
www.domain.com/page


I have searched for a solution alot but couldn't find one for vestacp :(

Re: Remove .PHP extension in NGINX?

Posted: Sat Oct 07, 2017 7:44 pm
by mehargags
That's a mod_rewrite feature, nothing exactly to do with VestaCP. You can do this by editing your .htaccess

Code: Select all

RewriteEngine  on
RewriteRule ^(.*)$ $1.php
Very detailed instructions and discussion here, you must read and test the different settings discussed.
Remove .php extension with .htaccess

Re: Remove .PHP extension in NGINX?

Posted: Mon Oct 09, 2017 8:53 am
by Phogo
What Megargags has suggested would work, but only on Apache based servers.

For nginx you would need something similar to the following,

Code: Select all

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;
}

Re: Remove .PHP extension in NGINX?

Posted: Mon Oct 09, 2017 11:41 am
by Nou4r
Phogo wrote:What Megargags has suggested would work, but only on Apache based servers.

For nginx you would need something similar to the following,

Code: Select all

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;
}
Thanks for the replies.
Im using NGINX - PHP-FPM, therefore the .htaccess method will not work for me.

When i use your code above (googled it and tried it before, tried it again now)
I get the following error:

Code: Select all

root@ub3r:~# nginx -t
nginx: [emerg] named location "@extensionless-php" can be on the server level only in /home/trash/conf/web/nginx.conf:21
nginx: configuration file /etc/nginx/nginx.conf test failed
I just fixed this, by adding the code to the end of my config file.
But now wen i access any page, the page automaticly gets downloaded instead of showign normally o.0
Image

Re: Remove .PHP extension in NGINX?

Posted: Mon Oct 09, 2017 2:21 pm
by Phogo
Do you have duplicate

Code: Select all

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

Re: Remove .PHP extension in NGINX?

Posted: Mon Oct 09, 2017 2:22 pm
by Nou4r
Phogo wrote:Do you have duplicate

Code: Select all

 location / {
    try_files $uri $uri.html $uri/ @extensionless-php;
    index index.html index.htm index.php;
No, i took care of this part too.

nevermind, i tried it again and now it worked o.0


do you have a idea, how i can automaticly remove the .php aswell as index?


Thanks alot for the support

Re: Remove .PHP extension in NGINX?

Posted: Mon Oct 09, 2017 2:38 pm
by skurudo