Nginx remove php extension

Vesta Control Panel — Forum

How can i remove the .php extensions from my urls?
For example:

I have searched for a solution alot but couldn’t find one for vestacp 🙁

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

Re: Remove .PHP extension in NGINX?

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

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: 60 Joined: Fri Dec 09, 2016 12:40 pm
Os: CentOS 6x Web: nginx + php-fpm

Re: Remove .PHP extension in NGINX?

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,

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
Re: Remove .PHP extension in NGINX?

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,

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

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:

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 

Image

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

Phogo Posts: 60 Joined: Fri Dec 09, 2016 12:40 pm
Os: CentOS 6x Web: nginx + php-fpm

Re: Remove .PHP extension in NGINX?

Post by Phogo » Mon Oct 09, 2017 2:21 pm

Источник

Remove .php extension with NGiNX

Removing .php or .html extension from your website URL when using NGiNX can be quite frustrating, luckily there is a quick and simple solution that we can use in order to fix this and no, it is not called .htaccess as you used to do it when using Apache as web server. It is a nice and elegant solution where you can manage this straight from your .conf file. In this short tutorial we will guide you step by step how to remove .php and .html extension from your URL when using NGiNX, this is a pretty good solution to boost your SEO and get some really nice and friendly website URLs.

Table of contents

Edit the .conf file

On our first step we’ll have to open and edit the appropriate configuration file. If you have no custom .conf file then you will typically the default file can be found at /etc/nginx/nginx.conf , please open it as shown in the example below using vi or whatever editor you like:

Remove .php and .html extensions from URL

On this step we have to add the following code lines inside the server block in order to remove the .php and .html extensions from your URLs, please use the below example:

 server < . 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; >. > 

Make sure you save the changes and lets move on to our next step.

Reload NGINX web server

Finally in order for our changes take effect we have to reload NGINX server, depending on your Linux distro you may have to use service [service_name] reload or systemctl reload [service_name] , we will use systemctl on this particular example:

That’s it, now simply open a browser and go to a URL without its .php or .html extension. For example, go to /index instead of /index.php and your browser should load the page, if that doesn’t work simply restart NGiNX, that should work.

Источник

Hide or Remove php or html File Extension In Nginx

It shows how to hide or remove the php or html extension by configuring the Nginx server block.

You might have seen the extensions including .php, .html, or .htm while accessing the web pages. It’s because of the webserver directly serving the file without hiding the extension. We can hide or remove the file extension from the URL by updating the server block.

Prerequisites

This tutorial assumes that the Nginx Web Server is already installed on either the local or remote system.

Update Server Block

This section shows the configuration required to hide the file extension from the URL. The extension could be either html or htm or php.

The default server block should be similar to the server block as shown below.

server listen 80; 
server_name example.com www.example.com;

root /var/www/example.com/html;

index index.html index.htm;

location / try_files $uri $uri/ =404;
>
>

Now update the serer block to hide the html extension from the URL as shown below.

# Update Server Block
sudo nano /etc/nginx/sites-available/example.com

# Update
server listen 80;
server_name example.com www.example.com;

rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
rewrite ^/(.*)/$ /$1 permanent;

root /var/www/example.com/html;

index index.html index.htm;

location / try_files $uri/index.html $uri.html $uri/ $uri =404;
>
>


# Save and Exit - Ctrl + o -> Enter -> Ctrl + x

# Reload Nginx
sudo systemctl reload nginx
# OR
sudo service nginx reload

Now try to access the HTML file without the .html extension. It should show the output of the HTML file.

Similar to the HTML, we can also hide the PHP extension by updating the server block. The below-mentioned example hides both html and php extensions from the URL.

server listen 80; 
server_name example.com www.example.com;

rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
rewrite ^/(.*)/$ /$1 permanent;

root /var/www/example.com/html;

index index.html index.htm index.php;

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

location @extensionless-php rewrite ^(.*)$ $1.php last;
>

# pass the PHP scripts to FastCGI
location ~ \.php$ try_files $uri =404;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
>

# deny access to .htaccess files
location ~ /\.ht deny all;
>
>

Summary

This tutorial provided the configurations required to hide the html extension or php extension or both extension from the URL by updating the server block of Nginx Web Server.

Источник

Nginx remove .php and .html file extension

im using NGINX Please help me with this : I mean : www.example.com/index.php I want : www.example.com/index My code : Solution 1: Create an .htaccess file in your working directory, then add this snippet of code. It basically takes the requested filename and removes the «.php» Solution 2: Create an .htaccess file in your working directory, then add this snippet of code.

Nginx remove .php and .html file extension

I’ve been trying to get nginx to work without .php or .html extensions, and I needed a bit of help. Thanks in advance for your time!

I tried the solution found at How to remove both .php and .html extensions from url using NGINX ? but it has no effect

Here’s my current nginx config. It’s a combination of several tutorials, but it works.

# You may add here your # server < # . # ># statements for each of your virtual hosts to this file ## # You should look at the following URL's in order to grasp a solid understanding # of Nginx configuration files in order to fully unleash the power of Nginx. # http://wiki.nginx.org/Pitfalls # ht tp://wiki.nginx.org/QuickStart # http://wiki.nginx.org/Configuration # # Generally, you will want to move this file somewhere, and start with a clean # file but keep this around for reference. Or just disable in sites-enabled. # # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. ## server < root /usr/share/nginx/www; index index.php index.html; server_name localhost; location / < try_files $uri $uri.php $uri.html $uri/ =404; >error_page 404 /404.html; # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ < # fastcgi_split_path_info ^(.+\.php)(/.+)$; # fastcgi_pass unix:/var/run/php5-fpm.sock; # fastcgi_index index.php; # include fastcgi_params; include fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.sock; >> # HTTPS server # #server < # listen 443; # server_name localhost; # # root html; # index index.html index.htm; # # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # # ssl_session_timeout 5m; # # ssl_protocols SSLv3 TLSv1; # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP; # ssl_prefer_server_ciphers on; # # location / < # try_files $uri $uri/ =404; # >#> 

You should add the following two lines, in the exact spots where you think it’d form a loop. 🙂

Due to the fact that $request_uri is always constant per request, and is not affected by other rewrites, it won’t, in fact, form any infinite loops.

#at the top of location / if ($request_uri ~ ^/(.*)\.html$) < return 302 /$1; >#within \.php$ if ($request_uri ~ ^/([^?]*)\.php($|\?)) < return 302 /$1?$args; >

Nginx — Rewrite url for php files, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

How to remove both .php and .html extensions from url

How to remove both .php and .html extensions from url using NGINX — PHP [ Ext for Developers : https://www.hows.tech/p/recommended. html ] How …

Remove .php extension from url in nginx

i have a nginx server running and want to remove the . php extension from my files. I have allready tried a few things but the only thing i managed to accopmplish was breaking the fastcgi proccessing leading into downloading php files. The server is running fine with the following configuration:

## # Virtual Host configuration for example.com ## server < listen 80; listen [::]:80; server_name example.com www.example.com; return 301 https://www.example.com$request_uri; >server < listen 443 ssl; listen [::]:443 ssl; ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GC$ ssl_prefer_server_ciphers on; ssl_dhparam /etc/letsencrypt/dhparams.pem; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; root /usr/share/nginx/html/example/; index index.php; server_name example.com www.example.com; location / < try_files $uri $uri/ =404; >location /uploads < deny all; >error_page 404 /templates/404.php; location ~ \.php$ < fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi.conf; fastcgi_intercept_errors on; >location ~* \.(?:ttf|ttc|otf|eot|woff|font.css|jpg|jpeg|png|gif|ico|css|js)$ < expires 365d; access_log off; add_header Cache-Control "public"; >location ~ /\. < deny all; >> 

Thank you for your effort and time.

## # Virtual Host configuration for example.com ## server < listen 80; listen [::]:80; server_name example.com www.example.com; return 301 https://www.example.com$request_uri; >server < listen 443 ssl; listen [::]:443 ssl; ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GC$ ssl_prefer_server_ciphers on; ssl_dhparam /etc/letsencrypt/dhparams.pem; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; root /usr/share/nginx/html/example/; index index.php; server_name example.com www.example.com; location / < try_files $uri $uri/ @extensionless-php; // add @extensionless-php >location /uploads < deny all; >error_page 404 /templates/404.php; location ~ \.php$ < try_files $uri =404; // add this fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi.conf; fastcgi_intercept_errors on; >location @extensionless-php < // add this block rewrite ^(.*)$ $1.php last; >location ~* \.(?:ttf|ttc|otf|eot|woff|font.css|jpg|jpeg|png|gif|ico|css|js)$ < expires 365d; access_log off; add_header Cache-Control "public"; >location ~ /\. < deny all; >> 

from this site http://www.tweaktalk.net/60/nginx- remove-php — file-extension -from-url

How to remove both .php and .html extensions from url, How to remove both .php and .html extensions from url using NGINX — PHP [ Ext for Developers : https://www.hows.tech/p/recommended. html ] How …

How to remove .php extensions in nginx config?

I know there are a lot of threads about this topic but none seemed to work for me. I am not sure if it is because I have to do it to both server blocks, or I was doing something wrong. Please help me out.

Below is my nginx config on the remote server on Amazon, the first block represent the backend and the second block represent the frontend:

limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:60m; # nginx 1.1.9 or higher limit_req_zone $binary_remote_addr zone=req_limit_per_ip:60m rate=20r/s; server < listen 8080; server_name api.commonskudev.com; root /var/www/api/public; gzip on; server_tokens off; index index.php; client_max_body_size 64M; fastcgi_read_timeout 300; fastcgi_send_timeout 300; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; send_timeout 300; try_files $uri $uri/ @rewrite-staging; location @rewrite-staging < rewrite ^ /index.php; >location ~* \.php$ < include fastcgi_params; # fastcgi_param HTTPS on; fastcgi_pass 127.0.0.1:9000; >> server < listen 443 default_server ssl; server_name rightsleeve.commonskudev.com; # rewrite ^ http://commonskudev.com/maintenance.html; ssl on; # ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem; # ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key; ssl_certificate /etc/nginx/csku-dev.crt; ssl_certificate_key /etc/nginx/csku-dev.key; gzip on; gzip_proxied any; client_max_body_size 64M; fastcgi_read_timeout 300; fastcgi_send_timeout 300; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; send_timeout 300; root /var/www/web; server_tokens off; index index.php; error_page 404 /404.php; rewrite ^/v1+\.1+/(.*)$ /$1; rewrite ^/project/(3+) /project.php?job_number=$1; if ($http_referer ~* "semalt\.com") < return 444; >location ~* ^(css|js|images|files) < expires 1y; add_header Pragma public; add_header Cache-Control public; >location ~* \.(ttf|woff) < add_header Access-Control-Allow-Origin "*"; >location ~* \.php$ < if (!-f $document_root/$fastcgi_script_name) < return 404; >limit_conn conn_limit_per_ip 35; limit_req zone=req_limit_per_ip burst=35; include fastcgi_params; fastcgi_param HTTPS on; fastcgi_pass 127.0.0.1:9000; > location ~* \.(png|jpg|dst|xls) < try_files $uri $uri/ @nofile; >location @nofile < rewrite ^ /images/404.png; >if ($uri ~* ^/([a-zA-Z0-9_\-]+)$) < rewrite ^/([a-zA-Z0-9_\-]+) /vanity_url.php?mask=$1&$args; >location /v1 < proxy_pass http://api.commonskudev.com:8080; proxy_set_header Host $host; >> server < listen 80 default_server; server_name rightsleeve.commonskudev.com; rewrite ^(.*) https://$host$1 permanent; >

You can use a rewrite similar to this:

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

As taken from this SO answer.

server < listen 80; server_name www.example.local; root /var/www/vhosts/example/httpdocs; index index.html index.htm index.php; location / < try_files $uri $uri/ @ext; >location ~ \/\.php < rewrite "^(.*)\/.php" $1.php last; >location ~ \.php$ < include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.4-fpm.sock; >location @ext < rewrite "^(.*)$" $1.php; >> 

Remove php extension nginx mamp pro 6, I’m attempting to remove the .php extension from the URL of my website in the NGINX config. This is my first time using both NGINX and MAMP Pro 6, so forgive me if the answer seems obvious (I’m use

Removing .php extension from URL address

I have reviewed the previous headings, but I do not fully understand and I can not fully do this remove php extension from url, the file is automatically downloaded when I try to do it like the other headers about remove extension I know these code are wrong.

 server < listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; server_name 45.**.91.84; location / < try_files $uri $uri/ /upload.php?$args; >location ~ \.php$ < include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; >location ~ /\.ht < deny all; >> 

Create an . htaccess file in your working directory, then add this snippet of code. It basically takes the requested filename and removes the «.php»

RewriteEngine On RewriteCond % !-f RewriteRule ^([^/]+)/$ $1.php RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php RewriteCond % !-f RewriteCond % !-d RewriteCond % !(\.[a-zA-Z0-9]|/)$ RewriteRule (.*)$ /$1/ [R=301,L] 

Create an .htaccess file in your working directory, then add this snippet of code. It basically takes the requested filename and removes the «.php»

RewriteCond %.php -f RewriteRule !.*\.php$ %.php [QSA,L] 

Create htaccess file and paste this code. Hope this will help you.

RewriteEngine On RewriteCond % !-f RewriteRule ^([^.]+)$ $1.php [NC,L]

Options +FollowSymLinks -MultiViews

Write above code in .htaccess file.

How do I remove the file extension from URLs in nginx?, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

Источник

Читайте также:  Php фреймворк для начинающих
Оцените статью