【问题标题】:How to hide my .html and .php extensions using .htaccess?如何使用 .htaccess 隐藏我的 .html 和 .php 扩展名?
【发布时间】:2019-04-21 15:40:38
【问题描述】:

我有这个 .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.php


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.html

问题是:它只是隐藏了 .php 不应该隐藏.php和.html吗?

【问题讨论】:

标签: .htaccess web


【解决方案1】:

将您的规则更改为:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

【讨论】:

    【解决方案2】:

    1./ 你的 html 文件仍然可以访问,因为你的第二组规则永远不会被使用。此外,您的第二行明确告诉服务器将“真实”文件放在一边。

    2./ 你只需要 RewriteEngine On 一次。

    3./ 你应该使用标志来告诉你的重写何时停止。

    RewriteEngine On
    # if the requested file does not exist
    RewriteCond %{REQUEST_FILENAME} !-f
    # if the requested folder does not exist
    RewriteCond %{REQUEST_FILENAME} !-d
    # sends all urls except home to its corresponding php file
    RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.php [R=301,L]
    

    但如果您希望它适用于所有文件扩展名,请删除第一条规则,只留下。

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.php [R=301,L]
    

    【讨论】:

    • 请小心 301 重定向,这可能会导致 SEO 出现重大问题,而且 IMO 不是这里所要求的
    • 在我看来这是有道理的,如果您通过 htaccess 重定向(通常不是您每天都会更改的文件),您希望 SE 将您的重定向作为合法 url。跨度>
    猜你喜欢
    • 1970-01-01
    • 2013-03-13
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    相关资源
    最近更新 更多