【问题标题】:.htaccess hide page extension.htaccess 隐藏页面扩展名
【发布时间】:2017-09-24 02:23:19
【问题描述】:

每当我尝试使用.htacess 隐藏.php 扩展名并转到使用.php 扩展名的页面时,屏幕上显示的所有内容都是“找不到页面”。我以前可以完美运行,但我最近更换了托管公司,现在我使用 Plesk Onyx 17.0.17CentOS Linux 7.3.1611PHP 7.1.4

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

Options -Multiviews -Indexes

# Disable Automatic Directory detection
DirectorySlash Off

ErrorDocument 404 /!404.php

RewriteEngine On
RewriteBase /

# Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/+$ $1 [R=301,L]

RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

【问题讨论】:

  • 是使用apache还是nginx。如果是使用 nginx,这个文件什么都不做。
  • @AndrewRayner mod_rewrite 是一个 Apache 模块
  • @PedroLobito 是的,但他说当他切换主机时它停止工作。这意味着如果他以前使用过apache。这会奏效的。
  • 我正在使用 Apache,也可能使用 Nginx。但是,我可以确认我的.htaccess 文件正在被使用,因为我已经尝试对其进行更改,这是我设法让我的自定义 404 页面正常工作的唯一方法。此外,由于 .php 扩展名,我认为可以防止使用 Additional directives for HTTPSAdditional nginx directives 设置我的 404 页面,因此必须使用 .htaccess 来完成。
  • 如果我将垃圾放在我的 .htaccess 文件的顶部,我会收到 500 错误。但是,如果我注释掉这行RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L],重定向仍然会执行。

标签: php .htaccess mod-rewrite plesk


【解决方案1】:

感谢Anubhava'sthis question 上的回答,我现在可以正常工作了。我仍然不知道为什么我的旧不能工作。

这是我用来解决问题的代码:

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

【讨论】:

    猜你喜欢
    • 2012-07-04
    • 2011-05-06
    • 2011-01-08
    • 1970-01-01
    • 2014-03-29
    • 2013-03-13
    • 2010-12-18
    • 2017-01-29
    相关资源
    最近更新 更多