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]```
2
Answers
I just wanted to post the working solution in case it helps anyone. The answer provided by Amit Verma gave me part of the solution, but unfortunately all my links were now being forced to redirect to a ".php" extension. My assumption is that something is going awry in my code, and that Amit Verma's solution wasn't to blame. But since I couldn't isolate why my URLs were now ".php", I played around with it until I got the ".html" extension I required. The syntax may not be totally correct, so if someone wants to offer any suggestions to make it better, please don't hesitate.
#You can use the following rule to redirect and rewrite your
.html
extension to.php
.This will redirect all your
php
files tohtml
extension changing your original file path from/file.php
to/file.html
.The
RewriteCondition
aboveRewriteCond %{ENV_REDIRECT_STATUS} !200
is important to avoid the infinite rewrite loop .Replace your rules with this :
`