Warm tip: This article is reproduced from serverfault.com, please click

Conflicting redirects in apache .htaccess rules, SSL redirect

发布于 2020-11-29 02:37:05

28 November 2020 02:58

Hello,

I have a question regarding configuring rules in the .htaccess file for Apache. I have recently installed an SSL certificate on my website, and am therefore attempting to redirect all of my http:// URLs to https://, however I am running into an issue. The URL rewrite rule is conflicting with another rule on my file and throwing a 403 error. I'm pretty sure I know which rule is causing the conflict, however I'm unsure of how to remedy it. One of my rules changes all .php extensions into .html extensions. Every time the file rewrites from http:// to https://, it automatically redirects to the .php file (which, because of how it's programmed, will throw an error if the .php file is accessed directly). For instance, if someone goes to http://www.mylink.info/about.html, it will redirect to https://www.mylink.info/about.php, throwing the error. How do I program it properly so that the file redirects properly to the .html extension? I'll paste my entire .htaccess code below. You can see my attempted solution in the last 5 lines.

# 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]```
Questioner
seaofinformation
Viewed
0
Amit Verma 2020-11-29 13:18:12

#You can use the following rule to redirect and rewrite your .html extension to .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]

This will redirect all your php files to html extension changing your original file path from /file.php to /file.html . The RewriteCondition above RewriteCond %{ENV_REDIRECT_STATUS} !200 is important to avoid the infinite rewrite loop .

Replace your rules with this :

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] 

`