[英]Restrict direct access to .php files through browser and allow only index.php to access them
[英]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規則的有限了解,這正在做兩件事:
http://
重定向到https://
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.