【问题标题】:htaccess remove .php and .html from urlhtaccess 从 url 中删除 .php 和 .html
【发布时间】:2017-08-11 14:14:19
【问题描述】:

我遇到了一个问题,即 google 索引了一些 URL 错误的页面。

他们正在编入索引的 URL 是:

http://www.example.com/user/emp.php

和 HTML URL:

 http://www.example.com/login.html

我需要它重定向到:

http://www.example.com/user/emp

和 HTML URL:

http://www.example.com/login

这是我的 .htaccess 文件

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

【问题讨论】:

标签: php html apache .htaccess mod-rewrite


【解决方案1】:

首先检查是否存在以.html.php结尾的适当目标,然后重写该URL

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html [L]

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

【讨论】:

  • 不需要NC 标志,因为模式不包含任何特定于大小写的字符。
  • @OlafDietsche 感谢您的意见 :)
【解决方案2】:
  # Remove .html-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.html -f
  RewriteRule ^([^\.]+)$ $1.html

【讨论】:

    【解决方案3】:

    在您的 htacess 文件中用此代码替换它。

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

    例如,请从您调用的所有链接中删除 .html 和 .php。

        <a href="index.html">Home</a>
    
         To
    
          <a href="index">Home</a>
    

    【讨论】:

    • 为什么从链接中删除 .html 实际上让我感到困惑。重写发生在服务器端。这与从链接中删除 .html 有何关系?
    • 在标签上解决问题但菜单显示时间出现404错误
    • 请确保您的文件存在于那里并且您提供了正确的路径。
    【解决方案4】:

    确保您已安装 mod_rewrite 模块,检查phpinfo(); 函数的输出以确认。我认为以下.htaccess 很好。将 htaccess 放在应用程序的根目录中。

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

    阅读This 了解如何在 php 中安装 mod_rewrite

    【讨论】:

    • 复制无效规则并不能回答问题。
    猜你喜欢
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多