简体   繁体   中英

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

I have the following .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:

  1. Redirecting http:// to https://
  2. Allow access of domain.com/file.php files as 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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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