首页 > 解决方案 > 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]```

标签: phpapache.htaccessssl

解决方案


#您可以使用以下规则将您的.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. 以上对于避免无限重写循环很重要RewriteConditionRewriteCond %{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] 

`


推荐阅读