访问PHP文件而不键入扩展名,但也允许文件夹默认为index.php

时间:2018-04-27 03:59:55

标签: php apache .htaccess

我有以下Rectangle

.htaccess

由于我对Apache htaccess规则的理解有限,这有两件事:

  1. RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] 重定向到http://
  2. 允许https://
  3. 访问domain.com/file.php个文件

    问题是规则#2认为每次对任何domain.com/file的调用都是/name,有时候是一个实际的文件夹,我需要它通过以下方式重定向到该文件夹​​的name.php文件默认值。

    有什么想法吗?

3 个答案:

答案 0 :(得分:0)

删除最后一行并尝试替换为:

RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

答案 1 :(得分:0)

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

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

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

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_FILENAME}.html [L]

## Redirect to HTTPS. Remove if you do not want this.
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

## Disables indexing (Always a good idea :D)
Options -Indexes

答案 2 :(得分:0)

在重写之前,您需要检查%{REQUEST_FILENAME }是否存在为现有的php文件:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https: //%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]