简体   繁体   English

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

[英]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规则的有限了解,这正在做两件事:

  1. Redirecting http:// to https:// http://重定向到https://
  2. Allow access of 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.

相关问题 限制通过浏览器直接访问 .php 文件,只允许 index.php 访问它们 - Restrict direct access to .php files through browser and allow only index.php to access them 使用包含&文件夹而不是命名的php文件的index.php - Using index.php with includes & folders instead of named php files Codeigniter url与index.php一起使用,也没有index.php - Codeigniter urls working with index.php and also without index.php .htaccess 脚本删除。php 扩展阻止索引。php 访问 - .htaccess script to remove .php extension prevents index.php access 隐藏php文件的扩展名并用htaccess隐藏index.php - Hide extension of php files and hide index.php with htaccess 使用htaccess将带有/不带.php扩展名的URL重定向到index.php - Redirect url with/without .php extension to index.php using htaccess 显示没有PHP扩展名的URL(/index.php/dashboard) - Show URL (/index.php/dashboard) without PHP extension 拒绝直接访问文件夹或文件,但允许index.php访问它 - Deny direct access to a folder or file, but allow index.php to access it 除了现有文件和文件夹之外,Apache将all重定向到index.php - Apache redirect all to index.php except for existing files and folders 如何密码保护index.php和/,但允许访问所有其他文件和子目录。 - How to password protect index.php and /, but allow access to all other files and subdirectories.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM