Forcing https:// through htaccess - apache

I'm very new to coding in general so please forgive me for my ignorance in the subject. I understand that their is a few questions similar to this, but none have seemed to work for me. Useful information is always welcome please due you're best to explain everything to me even if it seems a bit excessive because I'm fairly uneducated in theses particular subjects.
Here is my entire htaccess file located in my root forum directory.
RewriteOptions inherit
RewriteEngine on
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /forums/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /forums/public/404.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forums/index.php [L]
</IfModule>
<Files 403.shtml>
order allow,deny
allow from all
</Files>
RewriteCond %{HTTP_HOST} ^perspectiverp\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.perspectiverp\.com$
RewriteRule ^home$ "https\:\/\/www\.perspectiverp\.com\/forums\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^perspectiverp\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.perspectiverp\.com$
RewriteRule ^Home$ "https\:\/\/www\.perspectiverp\.com\/forums\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^perspectiverp\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.perspectiverp\.com$
RewriteRule ^/?$ "https\:\/\/www\.perspectiverp\.com\/forums\/" [R=301,L]
My site will always connect to the website through https:// unless it is being directly told to do otherwise e.g. http://perspectiverp.com/forums/. My issue is that as my general connection is being directed through https:// users can still connect through http:// which is a major security flaw. I basically need it to be similar to how google has their system to automatically redirect http:// to https:// even if the user attempts to change it in the web address
e.g. http:/ww.google.com/ gets redirected to https:/ww.google.com/?gws_rd=ssl if the user attempts to change it to*http:/ww.google.com/.
As for anyone wanting to know what the forum software is, I'm currently using a Licensed version of Invision Powers Community Suit.
// UPDATED
First attempt Result was "Webpage has Redirect Loop"
RewriteOptions inherit
RewriteEngine on
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /forums/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /forums/public/404.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forums/index.php [L]
</IfModule>
<Files 403.shtml>
order allow,deny
allow from all
</Files>
RewriteCond %{HTTPS} =off
RewriteRule ^ https://www.perspectiverp.com%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?perspectiverp\.com$ [NC]
RewriteRule ^(home)?$ https://www.perspectiverp.com/forums/ [R=301,NC,L]
Second attempt Result was "Webpage has Redirect Loop"
RewriteOptions inherit
RewriteEngine on
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /forums/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /forums/public/404.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forums/index.php [L]
</IfModule>
<Files 403.shtml>
order allow,deny
allow from all
</Files>
RewriteCond %{HTTPS} =off
RewriteRule ^ https://www.perspectiverp.com%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?perspectiverp\.com$ [NC]
RewriteRule ^home$ https://www.perspectiverp.com/forums/ [R=301,NC,L
Third attempt Result was "Webpage has Redirect Loop"
RewriteOptions inherit
RewriteEngine on
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /forums/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /forums/public/404.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /forums/index.php [L]
</IfModule>
<Files 403.shtml>
order allow,deny
allow from all
</Files>
RewriteCond %{HTTPS} =off
RewriteRule ^ https://www.perspectiverp.com%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^perspectiverp\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.perspectiverp\.com$
RewriteRule ^home$ "https\:\/\/www\.perspectiverp\.com\/forums\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^perspectiverp\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.perspectiverp\.com$
RewriteRule ^Home$ "https\:\/\/www\.perspectiverp\.com\/forums\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^perspectiverp\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.perspectiverp\.com$

Add the following just before your last three https:// rewrite rules.
RewriteCond %{HTTPS} =off
RewriteRule ^ https://www.perspectiverp.com%{REQUEST_URI} [R,L]
Once everything works as expected change R to R=301 above. Also, notice how the URL in my rewrite rule does not quote and escape any forward slashes as it isn't required.
You can also replace your last three rules with just this one:
RewriteCond %{HTTP_HOST} ^(www\.)?perspectiverp\.com$ [NC]
RewriteRule ^(home)?$ https://www.perspectiverp.com/forums/ [R=301,NC,L]
The NC flag makes the rule case-insensitive so home and Home both will match. The ? makes the preceding group () optional; so the RewriteCond works with both www and without it, and the RewriteRule matches on / as well.

I forced https:// through a reverse proxy server (Cloudflare) and was able to force my website to accept https:// traffic only.

Related

mod_rewrite rule for "www to non www" doesn't redirect sub-pages

I have made a thorough search before I have asked this question here. Please hear me out:
I am trying to redirect my blog from www to non www and it doesn't redirect any sub-pages. I have an http > https redirect in place as well and it works perfectly for both domain as well as the sub-pages. Here are the rules I have in my .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^^rcp-pep-ipn //?rcp-pep-listener=IPN [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
# END WordPress
I'd really appreciate an explanation if I am doing anything wrong here. I have literally pulled my hair since I have used the exact same code (from the second RewriteBase /) for all other sites and it worked flawlessly.
You should bring those protocol checking conditions to the beginning. You have some problems within the rules too. Try:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,NE,L]
RewriteRule ^rcp-pep-ipn /?rcp-pep-listener=IPN [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Apache Redirect certain HTTPS requests to HTTP not working

Hi I have been working on this for a while and just know I am doing something stupid but just can not see it. What I want to do is to redirect certain pages so they are always shown using HTTPS and all other pages if they are requested through HTTPS they are redirected as HTTP.
I have done lots of searching around the issue and found accepted answers on Stackoverflow that just did not work for me. So here Is my .htaccess file.
RewriteCond %{HTTPS} on
RewriteCond ${REQUEST_URI} !(finance/enquiryForm)(.*)
RewriteCond ${REQUEST_URI} !(offerentry)(.*)
RewriteRule (.*) http://dev.staging.co.uk/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(finance/enquiryForm|offerentry)(.*) https://dev.staging.co.uk/$1 [L,R=301]
What is currently happening is that the pages that I want to redirect from https to http work fine but the pages I want to show as HTTPS get a redirect loop and fail. If I comment out the top section the HTTPS pages I do want redirect fine. So how do I get the redirect loop to stop?
EDIT
The full .htaccess file with the codeigniter etc stuff.
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
<Files .*>
Order Deny,Allow
Deny From All
</Files>
#RewriteCond %{HTTPS} on
#RewriteCond ${REQUEST_URI} !/(finance/enquiryForm|offerentry) [NC]
#RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=302,NE]
#RewriteCond %{HTTPS} off
#RewriteCond ${REQUEST_URI} /(finance/enquiryForm|offerentry) [NC]
#RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302,NE]
# Protect application and system files from being viewed
RewriteRule ^(fuel/install/.+|fuel/crons/.+|fuel/data_backup/.+|fuel/codeigniter/.+|fuel/modules/.+|fuel/application/.+|\.$
# Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_URI} !fuel/.*$
RewriteRule ^(.*)/$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [L]
</IfModule>
Options -Indexes
Try these rules based on THE_REQUEST variable:
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/(finance/enquiryForm|offerentry) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=302,NE]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} /(finance/enquiryForm|offerentry) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302,NE]
Test them in Chrome dev tool and see what redirects are you getting.
Try with:
RewriteCond %{HTTPS} on
RewriteCond ${REQUEST_URI} !(finance/enquiryForm|offerentry)
RewriteRule (.*) http://dev.staging.co.uk/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule (finance/enquiryForm|offerentry) https://dev.staging.co.uk%{REQUEST_URI} [NE,L,R=301]

not getting slash / on my website url

My .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
#non www redirection
RewriteEngine On
RewriteCond %{HTTP_HOST} !^ruggedtrailsnepal\.com
RewriteRule (.*) http://ruggedtrailsnepal.com/$1 [R=301,L]
i am not getting / (slash) on some url , but some url's are working , how to solve this problem
link with problem
ruggedtrailsnepal.comtravel-info/visa-information.html
link without problem
ruggedtrailsnepal.com/company-info/about-us.html
Reorder your rules and use REQUEST_URI variable:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
#non www redirection
RewriteCond %{HTTP_HOST} !^ruggedtrailsnepal\.com$ [NC]
RewriteRule ^ http://ruggedtrailsnepal.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Better test this in a new browser to avoid old caches.

Redirecting HTTP to HTTPS correctly in Question2Answer script

I have searched for this question for many days and tried so many different methods but nothing works so far. I am using the Question2Answer script and I want to redirect all the HTTP requests to HTTPS.
My URL structure is set to :
/123/why-do-birds-sing (requires htaccess file)
and the htaccess file is as follow:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
</IfModule>
This correctly redirects http://example.com to https://example.com. However, if the user enters an address like this : www.example.com/users they are redirected to https://example.com/index.php?qa-rewrite=users which returns a 404 error.
The index.php?qa-rewrite= is added automatically and removing it from the htaccess totally messes up everything and I think it should be there.
This is because you need all of your redirect rules before any of the routing rules (the ones without the R flag), so:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
I use this:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ "https\:\/\/example\.com\/$1" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
If you don't like to redirect www, you can remove the line
RewriteCond %{HTTP_HOST} ^www\.example\.com$ and the [OR] above

.htaccess subdirectory redirect with non www to www and parameters

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /qsg/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?Item=$1 [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /qsg/
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?Item=$1 [L]
</IfModule>
I tried to look for an answer, but haven't been able to find one.
I am trying to redirect:
http://example.com/qsg/abcd
to
http://www.example.com/qsg/index.php?Item=abcd
I can get this working fine when the www is present in the URI. When I remove the www, it redirects to http://www.example.com/404.shtml
I've tried the two methods above with both the same result. I just can't figure out what I am doing wrong. I've seen plenty of other examples where this should work, but not for me. Do I have something wrong in my .htaccess file is there a possibility of something else causing the bad redirect?
Note: both the above rewrites are not included in the file. I've tried both independently.
Give this one a shot:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /qsg/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?Item=$1 [L,PT]
</IfModule>