php - apache .htaccess 规则中的冲突重定向、SSL 重定向
问题描述
2020 年 11 月 28 日 02:58
你好,
我有一个关于在 .htaccess 文件中为 Apache 配置规则的问题。我最近在我的网站上安装了 SSL 证书,因此我试图将我的所有 http:// URL 重定向到 https://,但是我遇到了问题。URL 重写规则与我文件上的另一个规则冲突并引发 403 错误。我很确定我知道哪个规则导致了冲突,但是我不确定如何解决它。我的一条规则将所有 .php 扩展名更改为 .html 扩展名。每次文件从 http:// 重写为 https:// 时,它都会自动重定向到 .php 文件(由于它的编程方式,如果直接访问 .php 文件,则会引发错误)。例如,如果有人访问http://www.mylink.info/about.html,它将重定向到https://www.mylink.info/about.php,抛出错误。如何正确编程以使文件正确重定向到 .html 扩展名?我将在下面粘贴我的整个 .htaccess 代码。您可以在最后 5 行中看到我尝试的解决方案。
# Set the “ea-php55” package as the default “PHP” programming language.
AddType application/x-httpd-ea-php55 .php .php5 .phtml
# php -- END cPanel-generated handler, do not edit
suPHP_ConfigPath /home/myusername
order allow,deny
deny from all
AddType video/ogg .ogv
AddType video/mp4 .mp4
AddType video/webm .webm
AddType audio/mpeg .mp3
AddType audio/ogg .ogg
AddType audio/mp4 .m4a
AddType audio/wav /wav
Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
# BEGIN Compress text files
AddOutputFilterByType DEFLATE text/html text/xml text/css text/plain
AddOutputFilterByType DEFLATE image/svg+xml application/xhtml+xml application/xml
AddOutputFilterByType DEFLATE application/rdf+xml application/rss+xml application/atom+xml
AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript application/json
AddOutputFilterByType DEFLATE application/x-font-ttf application/x-font-otf
AddOutputFilterByType DEFLATE font/truetype font/opentype
# END Compress text files
# BEGIN Expire headers
ExpiresActive On
ExpiresDefault "access plus 5 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
# END Expire headers
# BEGIN Cache-Control Headers
Header set Cache-Control "public"
Header set Cache-Control "public"
Header set Cache-Control "private"
Header set Cache-Control "private, must-revalidate"
# END Cache-Control Headers
# BEGIN Turn ETags Off
FileETag None
# END Turn ETags Off
AddType text/x-component .htc
AddHandler application/x-httpd-php .js
DirectoryIndex index.php
RewriteEngine on
# block direct access to .php files
RewriteCond %{THE_REQUEST} \.php [NC]
RewriteRule \.php$ - [F,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)\.html$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/favicon\.ico
RewriteRule ^ mycontroller.php [L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^mylink\.info$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mylink\.info$
RewriteRule ^(.*)$ "https\:\/\/www\.mylink\.info\/$1" [R=301,L]```
解决方案
#您可以使用以下规则将您的.html
扩展重定向并重写为.php
.
RewriteEngine on
#redirect .html to .php
RewriteCond %{ENV_REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.html$ /$1.php [L,R]
#internally map file.html to file.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)\.html$ /$1.php [L]
这会将您的所有php
文件重定向到html
扩展名,将您的原始文件路径从更改/file.php
为/file.html
. 以上对于避免无限重写循环很重要RewriteCondition
。RewriteCond %{ENV_REDIRECT_STATUS} !200
用这个替换你的规则:
RewriteEngine on
#http to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^mylink\.info$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mylink\.info$
RewriteRule ^(.*)$ "https\:\/\/www\.mylink\.info\/$1" [R=301,L]
#redirect .html to .php
RewriteCond %{ENV_REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.html$ /$1.php [L,R]
#internally map file.html to file.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)\.html$ /$1.php [L]
######################
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/favicon\.ico
RewriteRule ^ mycontroller.php [L]
`
推荐阅读
- azure - 是否可以从 cosmos db azure 中删除记录
- c# - 从 Web 请求响应中解析 json
- python - 在python中计算标准输入的平均值
- php - Laravel Jetstream - 是否需要 Sanctum 来保护网络路由?
- .net - VB.NET:杰出事件
- json - 如何将表数据转换为 json 对象
- javascript - 如何在 THREE.SphereGeometry 上获取纹理图像的触摸位置
- javascript - 使用 jQuery 从 url 参数中保存 utm 值
- nativescript - 在 NativeScript 中如何访问 IOS 上的外部存储路径
- angular-directive - 如何在 Angular 10 中使用自定义指令设置元素的占位符样式?