【问题标题】:Nginx Rewrite to remove php filename [duplicate]Nginx重写以删除php文件名[重复]
【发布时间】:2020-04-06 09:39:11
【问题描述】:

嘿嘿

是否可以在 Nginx 中编写用于删除文件名的重写规则?

我有这样的: domain.tld/post.php?p=1 并想要这样的东西: domain.tld/?p=1

有办法吗?如果是,如何?

谢谢

【问题讨论】:

    标签: php nginx url-rewriting


    【解决方案1】:

    是的,绝对的。

    将所有请求重定向到您的 index.php 文件,然后在您的应用程序逻辑中路由请求是一种相当普遍的做法 - 这样您的 PHP 代码可以完全定义您的 URL 结构。

    如果您想这样做,这里有一个示例 Nginx 重写配置,您可以使用它来重写所有对 index.php 的请求:

    #
    # Redirect all to index.php
    #
    RewriteEngine On
    
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
    RewriteRule .* index.php [L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 2020-06-08
      • 2011-08-29
      • 1970-01-01
      • 2013-01-12
      • 2018-05-25
      • 1970-01-01
      • 2014-09-19
      相关资源
      最近更新 更多