使用NGINX删除.php扩展名

I'm on a NGINX web server and I would like to remove the .php extension from url.

I currently have the following conf :

server {
    server_name www.mywebsite.com mywebsite.com;
    return 301 https://mywebsite.com$request_uri;
}
server {
    listen 443 ssl;

    server_name www.mywebsite.com;
    return 301 https://mywebsite.com$request_uri;

    ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
}
server {
    listen 443 ssl;
    root /opt/http/nginx/sites/mywebsite/www;
    index index.php index.html;
    server_name mywebsite.com;

    location / {
        #rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
        try_files $uri $uri/ $uri.php?$args;
    }

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 365d;
    }

    location ~*  \.(pdf)$ {
        expires 30d;
    }

    client_max_body_size 3M;

    ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    error_page 403 /index.php;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    access_log   /var/log/nginx/www.mywebsite.access.log;
    error_log   /var/log/nginx/www.mywebsite.error.log;
}

If tried to follow some similar case instructions but nothing seems to work with my conf.

The problem is that this piece of code

location / {
    try_files $uri $uri/ $uri.php?$args;
}

is working well in two cases :

  1. The client asks for https://mywebsite.com/page.php : OK
  2. The client asks for https://mywebsite.com/page : OK

without rewriting the url !

What I need is to tell NGINX to rewrite the url if the client tries to access a page with the file extension. For example, if I ask for login.php, nginx rewrites 'login', and so on.

It also has to keep the GET params in the url if it has.

So what is the right conf to do that, given that I keep in my code the links to the php files with extension and not the relative urls (I hope NGINX could rewrite them) ? (if I need to set the relative urls in my code I can but I don't want to break my local)

dsndm82062
dsndm82062 是的我按照这篇文章给出的说明但没有任何效果......
4 年多之前 回复
dongshuzui0335
dongshuzui0335 使用重写规则从url中删除.php的可能重复
4 年多之前 回复

2个回答



我已经尝试过这个,谢谢顺便说一句,但它不会重写所有网址。 例如:</ p>

 &lt; nav class =“navbar navbar-inverse navbar-fixed-top”&gt; 
&lt; div class =“container”&gt;
&lt; ; div class =“navbar-header”&gt;
&lt; button type =“button”class =“navbar-toggle collapsed”data-toggle =“collapse”data-target =“#navbar”aria-expanded =“false “aria-controls =”navbar“&gt;
&lt; span class =”sr-only“&gt;切换导航&lt; / span&gt;
&lt; span class =”icon-bar“&gt;&lt; / span&gt; \ n&lt; span class =“icon-bar”&gt;&lt; / span&gt;
&lt; span class =“icon-bar”&gt;&lt; / span&gt;
&lt; / button&gt;
&lt; a class =“navbar-brand”href =“index.php”&gt;
&lt; img height =“34”src =“img / logo.png”alt =“网站徽标”&gt;
&lt; / a&gt; \ n&lt; / div&gt;
&lt; div id =“navbar”class =“navbar-collapse collapse”&gt;
&lt; ul class =“nav navbar-nav navbar-right”&gt;
&lt;?php if(isset($ _ SESSION ['auth'])):?&gt;
&lt; li&gt;&lt; a href =“logout.php”&gt;退出&lt; / a&gt;&lt; / li&gt;
&lt;?php else:?&gt;
&lt; li&gt;&lt; a href =“注册 .php“&gt;唱歌&lt; / a&gt;&lt; / li&gt;
&lt; li&gt;&lt; a href =”login.php“&gt;登录&lt; / a&gt;&lt; / li&gt;
&lt;? php endif; ?&gt;
&lt; / ul&gt;
&lt; / div&gt;&lt;! - /。navbar-collapse - &gt;
&lt; / div&gt;
&lt; / nav&gt;
</ code> </ pre>

如果我点击徽标,它会重定向到 index.php </ code>,NGINX会将网址重写为 / index </ code>(仅限 / </ code>会更好,但为什么不......),所以这就是我正在寻找的行为。</ p>

但如果我点击“登录”,例如 ,它重定向到 login.php </ code>,没有其他任何事情发生。 但是如果我输入 / login </ code>,那么它可以工作,但似乎NGINX只重写 index.php </ code>文件,所以我需要在所有页面上。</ p> \ n </ div>

展开原文

原文

I already tried this one, thanks btw, but it doesn't rewrite all the urls. For example :

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="index.php">
                <img height="34" src="img/logo.png" alt="Website logo">
            </a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <?php if (isset($_SESSION['auth'])): ?>
                    <li><a href="logout.php">Logout</a></li>
                <?php else: ?>
                    <li><a href="register.php">Sing up</a></li>
                    <li><a href="login.php">Sign in</a></li>
                <?php endif; ?>
            </ul>
        </div><!--/.navbar-collapse -->
    </div>
</nav>

If I click on the logo, it redirects to index.php and NGINX rewrites the url to /index (only / will be better but why not...), so this is the behavior i'm looking for.

But if I click on "Sign In" for example, it redirects to login.php and nothing else happen. But if I enter /login, then it works but it seems that NGINX only rewrites the index.php file, so I need it on all pages.

doutian4046
doutian4046 只需添加重写^(/。+)\。php $ $ scheme:// $ host $ 1 permanent; 在服务器{}块中,它总是重写没有.php扩展名的url!
大约 4 年之前 回复



  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;
}
</ code> </ pre>

这将删除html和php表单网址。更多细节 </ p>
</ div>

展开原文

原文

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

This will remove both html and php form url .More Detail

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐