从URL地址中删除.php扩展名

时间:2017-09-14 12:28:54

标签: php nginx

我已经回顾了之前的标题,但我不完全理解,我不能完全做到这一点从url删除php扩展,当我尝试这样做时,文件会自动下载,就像其他标题有关删除扩展我知道这些代码错了。

我正在使用NGINX

请帮助我:

  • 我的意思是:www.example.com/index.php
  • 我想:www.example.com/index

我的代码:

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

}

4 个答案:

答案 0 :(得分:0)

在工作目录中创建.htaccess文件,然后添加此代码段。它基本上采用所请求的文件名并删除" .php"

//$counter should be set out here to 1...
//Start here with row don't have to worry about 1
echo '<div class="row">';    
while($row = mysqli_fetch_assoc($result)){                    

    //Other echo stuff...

    //Check if 3 yet
    if($counter == 3) {
        //reset to 0 cause you will incrment
        $counter = 0; 
        //end and start new div
        echo '</div>';
        echo '<div class="row">';
    } 
    $counter ++;
}
//Close it up unless it is always ending on 3?
//Probably want some more checking here somehow
echo '</div>';

答案 1 :(得分:0)

在工作目录中创建.htaccess文件,然后添加此代码段。它基本上采用所请求的文件名并删除“.php”

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

答案 2 :(得分:0)

创建htaccess文件并粘贴此代码。希望这会对你有所帮助。

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

答案 3 :(得分:0)

选项+ FollowSymLinks -MultiViews

上的RewriteEngine

RewriteEngine On

RewriteCond%{REQUEST_FILENAME}!-f

RewriteRule ^([^。] +)$ $ 1.php [NC,L]

将以上代码写在.htaccess文件中。