Vesta Control Panel - Forum

Community Forum

Skip to content

Advanced search
  • Quick links
    • Main site
    • Github repo
    • Google Search
  • FAQ
  • Login
  • Register
  • Board index Main Section Web Server
  • Search

Remove .PHP extension in NGINX?

Questions regarding the Web Server
Apache + Nginx, Nginx + PHP5-FPM
Post Reply
  • Print view
Advanced search
7 posts • Page 1 of 1
Nou4r
Posts: 35
Joined: Mon Feb 08, 2016 9:00 pm

Remove .PHP extension in NGINX?
  • Quote

Post by 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 :(
Top

mehargags
Support team
Posts: 1096
Joined: Sat Sep 06, 2014 9:58 pm
Contact:
Contact mehargags
Website Skype

Os: Debian 8x
Web: apache + nginx
Re: Remove .PHP extension in NGINX?
  • Quote

Post by 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
Top

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

Os: CentOS 6x
Web: nginx + php-fpm
Re: Remove .PHP extension in NGINX?
  • Quote

Post by 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;
}
Top

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

Re: Remove .PHP extension in NGINX?
  • Quote

Post by 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
Top

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

Os: CentOS 6x
Web: nginx + php-fpm
Re: Remove .PHP extension in NGINX?
  • Quote

Post by 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;
Top

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

Re: Remove .PHP extension in NGINX?
  • Quote

Post by 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
Top

skurudo
VestaCP Team
Posts: 8099
Joined: Fri Dec 26, 2014 2:23 pm
Contact:
Contact skurudo
Website Facebook Google+ Skype
Twitter

Re: Remove .PHP extension in NGINX?
  • Quote

Post by skurudo » Mon Oct 09, 2017 2:38 pm

https://stackoverflow.com/questions/219 ... sing-nginx
https://stackoverflow.com/questions/381 ... with-nginx
Top


Post Reply
  • Print view

7 posts • Page 1 of 1

Return to “Web Server”



  • Board index
  • All times are UTC
  • Delete all board cookies
  • The team
Powered by phpBB® Forum Software © phpBB Limited
*Original Author: Brad Veryard
*Updated to 3.2 by MannixMD
 

 

Login  •  Register

I forgot my password