[英]Access PHP files without typing extension but also allow folders default to index.php
I have the following .htaccess
: 我有以下.htaccess
:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To my limited understanding of Apache htaccess rules this is doing two things: 据我对Apache htaccess规则的有限了解,这正在做两件事:
http://
to https://
将http://
重定向到https://
domain.com/file.php
files as domain.com/file
允许将domain.com/file.php
文件作为domain.com/file
访问 The problem is that rule #2 thinks every call to anything /name
is a name.php
when sometimes is an actual folder, and I need it to redirect to that folder's index.php
file by default. 问题在于规则2认为有时对每个/name
调用都是一个name.php
,而有时是一个实际的文件夹,因此我需要默认情况下将其重定向到该文件夹的index.php
文件。
Any ideas? 有任何想法吗?
删除最后一行,然后尝试替换为:
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
You need to check if %{REQUEST_FILENAME }
exists as an existing php file before rewriting it: 您需要先检查%{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.