繁体   English   中英

无需输入扩展名即可访问PHP文件,但允许文件夹默认为index.php

[英]Access PHP files without typing extension but also allow folders default to index.php

我有以下.htaccess

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

据我对Apache htaccess规则的有限了解,这正在做两件事:

  1. http://重定向到https://
  2. 允许将domain.com/file.php文件作为domain.com/file访问

问题在于规则2认为有时对每个/name调用都是一个name.php ,而有时是一个实际的文件夹,因此我需要默认情况下将其重定向到该文件夹​​的index.php文件。

有任何想法吗?

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

RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
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

您需要先检查%{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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM