【问题标题】:Removing the file extension from URLs, like .php or .html从 URL 中删除文件扩展名,例如 .php 或 .html
【发布时间】:2017-12-13 05:36:15
【问题描述】:

我想从我网站的 URL 中删除我的任何文件扩展名。

例如:www.example.ml/dash/logins/sign.php
至:www.example/dash/logins/sign/

我做了很多谷歌搜索,但没有一个答案对我有用。 我认为它不起作用,因为在登录文件夹中我没有​​一个文件。

还有很多其他文件,例如 index.php、sign.php 等。我必须将每个文件保存在单独的文件夹中还是 sing.php 可以更改为 sign/。另外,这会发生什么:

sign.php?usercode=1

它会被转换成

sign/usercode=1

?

我已经在.htaccess中有一些代码,所以请在参考这个代码后给出你的答案。

php_value display_errors Off
php_flag magic_quotes 1
php_flag magic_quotes_gpc 1
php_value mbstring.http_input auto
php_value date.timezone America/New_York

Options -Indexes

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www.example.ml$ [NC]

RewriteRule ^(.*)$ https://www.example.ml/$1 [L,R=301]

【问题讨论】:

    标签: php html apache .htaccess server


    【解决方案1】:

    将此行添加到您的 .htaccess 文件中:

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

    【讨论】:

    • 谢谢它的工作,但在输入扩展名后它也正在打开,我想强制每个 url 删除它的扩展名
    • .htaccess 不能限制它。它只是重写你的网址。为此,您必须创建一个中间页面,其中 uri 将充当 get 参数,并基于此显示页面。这样做会花费更多。
    【解决方案2】:

    在您的 .htaccess 文件中使用此代码。它会将 PHP 文件重定向到非 PHP 版本。

    # Turn on the rewrite engine
    RewriteEngine  on
    # If the request doesn't end in .php (case insensitive) continue processing rules
    RewriteCond %{REQUEST_URI} !\.php$ [NC]
    # If the request doesn't end in a slash continue processing the rules
    RewriteCond %{REQUEST_URI} [^/]$
    # Rewrite the request with a .php extension. L means this is the 'Last' rule
    RewriteRule ^(.*)$ $1.php [L]
    

    示例取自 Tim Bielwa, Remove .php extension with .htaccess

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 1970-01-01
      • 2017-02-24
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      相关资源
      最近更新 更多