Remove .PHP extension in NGINX? Topic is solved

Questions regarding the Web Server
Apache + Nginx, Nginx + PHP5-FPM
Nou4r
Posts: 16
Joined: Mon Feb 08, 2016 9:00 pm

Remove .PHP extension in NGINX?

Postby Nou4r » Sat Oct 07, 2017 3:19 pm

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 :(

mehargags
Moderators
Posts: 787
Joined: Sat Sep 06, 2014 9:58 pm
Contact:

Re: Remove .PHP extension in NGINX?

Postby mehargags » Sat Oct 07, 2017 7:44 pm

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

Phogo
Posts: 37
Joined: Fri Dec 09, 2016 12:40 pm

Re: Remove .PHP extension in NGINX?  Topic is solved

Postby Phogo » Mon Oct 09, 2017 8:53 am

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

Nou4r
Posts: 16
Joined: Mon Feb 08, 2016 9:00 pm

Re: Remove .PHP extension in NGINX?

Postby Nou4r » Mon Oct 09, 2017 11:41 am

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

Phogo
Posts: 37
Joined: Fri Dec 09, 2016 12:40 pm

Re: Remove .PHP extension in NGINX?

Postby Phogo » Mon Oct 09, 2017 2:21 pm

Do you have duplicate

Code: Select all

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

Nou4r
Posts: 16
Joined: Mon Feb 08, 2016 9:00 pm

Re: Remove .PHP extension in NGINX?

Postby Nou4r » Mon Oct 09, 2017 2:22 pm

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



Return to “Web Server”



Who is online

Users browsing this forum: Bing [Bot] and 5 guests