【问题标题】:How can I hide .php from displaying in the address bar?如何隐藏 .php 使其不显示在地址栏中?
【发布时间】:2014-03-26 17:53:41
【问题描述】:

我有一个带有 html 页面和一个 .php 页面的网站。我正在尝试找出一种从地址栏中隐藏 .php 扩展名的方法。

我希望页面显示为 example.com/submit,而不是 example.com/submit.php

此外,使用下面的 .htaccess 文件,我似乎无法获得从 .html 版本页面重定向到 .php 页面的 google 站点链接。

这是我的 .htaccess 文件中的内容:

ErrorDocument 404 /page-not-found.html

ErrorDocument 403 /access-denied.html

SetEnv TZ America/Chicago

IndexIgnore *

Options +MultiViews

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

我希望有人可以帮助我完成这项工作。

谢谢。

看来这个答案来自另一个特定于godaddy作品的线程。甚至不是接受的答案,而是附加评论。

Using .htaccess to make all .html pages to run as .php files?

Options +ExecCGI
AddType application/x-httpd-php .php .html
AddHandler x-httpd-php5 .php .html

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    您可以使用这些规则:

    ErrorDocument 404 /page-not-found.html
    ErrorDocument 403 /access-denied.html
    SetEnv TZ America/Chicago
    
    IndexIgnore *
    Options -MultiViews
    
    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
    RewriteRule ^ /%1 [R=301,L,NE]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
    RewriteRule ^(.+?)(?:.html)?$ /$1.php [L,NC]
    

    【讨论】:

    • 内部服务器错误 服务器遇到内部错误或配置错误,无法完成您的请求。请联系服务器管理员并告知他们错误发生的时间,以及您在此错误之前执行的操作。服务器错误日志中可能会提供有关此错误的更多信息。
    • 您是否可以检查您的 Apache 错误日志以了解此错误的原因。
    • 我使用 Godaddy 作为我的托管服务提供商,如果有帮助的话。我听说如果 Godaddy 是主持人,则需要“Options MultiViews”。
    • 其实我有一个错字。我刚刚修好了。你能再试一次吗?
    • 它不会重定向,而且当我访问 www.3elementsreview.com/submission-guidelines.php 的页面时,.php 扩展名仍然在地址栏中可见。
    【解决方案2】:

    front controller pattern 或许可以为您提供更好的帮助。

    这可以帮助您拥有 /foo/bar 并且所有内容都将由 index.php 处理。

    如果您可以使用 .html 而不是 .php,使用类似这样的方式编辑 apache 配置可能会有所帮助:

    AddType application/x-httpd-php .php .html .htm .your_ext
    

    【讨论】:

    • 那么如果我将这一行添加到我的 .htaccess 文件中,php 代码可以在 .html 中运行吗?
    • 是的,这样你就可以在anything.html里面有php代码,apache会像php文件一样“解释”它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 2016-10-23
    • 2014-02-28
    相关资源
    最近更新 更多