Force user to use https:// + www - apache

The target is the subdomain tc.gamingprouk.net and it is using valid SSL. I want .htaccess to add not only https:// or www., I want both of them! Like https://www.tc.gamingprouk.net.
This is what my .htaccess contains:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^tc.gamingprouk.net [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.tc.gamingprouk.net/$1 [R,L]
# Hiding file extensions
Options +MultiViews
RewriteEngine on
# For .php, .html & .shtml URL's:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.shtml [NC,L]
Here is the problem:
Case 1:
User navigates to -> tc.gamingprouk.net
.htaccess rewrites to -> https://tc.gamingprouk.net
The problem: There is no www.!
Case 2:
User navigates to -> www.tc.gamingprouk.net
.htaccess does nothing!
The problem: It is without https!
Case 3:
User navigates to -> https://www.tc.gamingprouk.net
.htaccess rewrites to the same
The problem: Only in this case it is with both https:// and www.

You can use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NE]
This checks to see if both www and HTTPs are on, if they're not, it will turn them on. Make sure you clear your browser cache before testing this.

Answered
I found out what you can Use. The following .htaccess code is part of google's:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
# Hiding file extensions
Options +MultiViews
RewriteEngine on
# For .php, .html & .shtml URL's:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.shtml [NC,L]

Related

htaccess - redirect everything to index.php except a folder and specific files

I have been fighting with this for way too long. I have a my PHP site hosted with a .htaccess file for rewriting and redirecting. It has been working great so far. Now I simply want to add a subfolder, /pp that everything inside it does not get redirected or rewritten, basically it should not get touched by my .htaccess stuff.
Now when I go to mysite.com/pp/test.php (that file does exist), it redirects to mysite.com/index
Here is what I currently have:
RewriteEngine On
#remove www for all traffic
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#prevent redirect of submit
RewriteRule ^/?submit$ submit.php [L]
#prevent redirect of paypal
RewriteRule ^/?admin$ admin.php [L]
RewriteRule ^/?testPPButton$ testPPButton.php [L]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/pp/.* [NC] // this line is not working for some reason
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Options -Indexes
Updated .htaccess:
RewriteEngine On
# skip /pp/* from all rules below
RewriteRule ^pp(/.*)?$ - [L,NC]
#remove www for all traffic
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#prevent redirect of submit
RewriteRule ^/?submit$ submit.php [L]
#prevent redirect of paypal
RewriteRule ^/?admin$ admin.php [L]
RewriteRule ^/?testPPButton$ testPPButton.php [L]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Options -Indexes
Just below RewriteEngine On line add this to skip pp/ and everything under this folder:
RewriteEngine On
# skip /pp/* from all rules below
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+pp[/?\s] [NC]
RewriteRule ^ - [L,NC]
# rest of your rules go below this

htaccess redirect all but three pages to https

I know this sounds like a duplicate but I have tried as many methods as I can find and I still have not gotten this to work. So please do not auto mark this as a duplicate.
I am trying to redirect all pages of http://www.domian.com to https://www.domain.com with the exception of three pages:/products/product1, /products/product2, /products/product3
I also want those pages to always direct to http since if an https page links to it via a relative link, it will naturally have the https already there.
Also in play is that this is an expression engine site where I am removing the index.php from the URI.
Current Htaccess, which results in those three pages still being https even I go to Http for those pages.
### secure .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
### EE 404 page for missing pages
ErrorDocument 404 /index.php/site/404
###Block Access to directories with no index file
Options -Indexes
### Simple 404 for missing files
<FilesMatch "(\.jpe?g|gif|png|bmp|css|js|flv)$">
ErrorDocument 404 "File Not Found"
</FilesMatch>
### Although highly unlikely, your host may have +FollowSymLinks enabled at the root level, yet disallow its addition in .htaccess; in which case, adding +FollowSymLinks will break your setup (probably a 500 error), so just remove it, and your rules should work fine.
###Options +FollowSymlinks
RewriteEngine On
RewriteBase /
###Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteRule ^index\.php(.+) $1 [R=301,L]
### Add the www
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
###each option I try I replace in this block as a reference
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/products/product3
RewriteCond %{REQUEST_URI} !^/products/product1
RewriteCond %{REQUEST_URI} !^/products/product2
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
######
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
###
Also have tried this which results in the same as above:
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} !^(domain\.com/products/product1|domain\.com/products/product2|domain\.com/products/product2) [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(domain\.com/products/product1|domain\.com/products/product2|domain\.com/products/product2) [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R,L]
Tried this alone just to see if would be able to navigate to http://www.domain.com/products/product1 and at least not have it rewrite, but it still rewrites to https
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !/products/product1 [NC,OR]
RewriteCond %{REQUEST_URI} !/products/product2 [NC,OR]
RewriteCond %{REQUEST_URI} !/products/product3 [NC,OR]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
Tried this as well, which again still writes those pages to https. Plus if I try to go to http , it rewrites to https and doesn't remove the index.php
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/products/product3
RewriteCond %{REQUEST_URI} !^/products/product1
RewriteCond %{REQUEST_URI} !^/products/product2
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^/products/product3 http://www.domain.com/products/product3 [R=301,QSA,L,NE]
RewriteRule ^/products/product1 http://www.domain.com/products/product1 [R=301,QSA,L,NE]
RewriteRule ^/products/product2 http://www.domain.com/products/product2 [R=301,QSA,L,NE]
I have honestly tried about 10 -15 versions of all this above and still cannot get it. Including adding index.php? to the request_URI in case that it sees that as part of the request_URI before it's removed. I either end up with the page still rewriting to https or redirect loop land.
Am I missing something simple, is something in the wrong order? I'm just at a loss.
thanks for any help.
UPDATE:
Here's what I used to get it to work:
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} (\s/products/product1|\s/products/product2|\s/products/product3) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/products/product1 [NC]
RewriteCond %{THE_REQUEST} !\s/products/product2 [NC]
RewriteCond %{THE_REQUEST} !\s/products/product3 [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Here's how we achieved this on a site with a webcam that was in an iframe that wouldn't work on SSL. This was also an ExpressionEngine site.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#force www instead of non-www
RewriteCond %{HTTP_HOST} ^domain.co.uk
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,L]
# HTTP/HTTPS handling
# Force HTTP for webcam page only
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/webcam-page [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# Force HTTPS for all pages except webcam
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/webcam-page [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [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]

htaccess to change url

I have the following code in my .htacess but it didn't work right. Is it because mod-rewrite is no "on", if so, how can i check?
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\$ $1.php [nc]
I wanted to rename my address, example:
http://www.abc.com -> http://www.abc.com
http://abc.com -> http://www.abc.com
http://www.abc.com/123.html -> http://www.abc.com/123
http://www.abc.com/12-12-12.html -> http://www.abc.com/12-12-12
http://subdomain.abc.com/123.html -> http://subdomain.abc.com/123
Basically removing the extension and ensuring that its www is intact.
Edited:
It was rewrote into
Options +FollowSymlinks
RewriteEngine on
RewriteCond % ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^(.*).php $1
but still not working
Step 1.
Change all of your links to have the .html removed from links. Make sure you have Multiviews turned off:
Options -Multiviews
Step 2.
You need rules to redirect the browser when there is a www missing from the hostname:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Step 3.
You need rules to redirect the browser when a URL is requested with .html or .php with the extensions removed:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.(php|html?)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%2 [L,R=301]
Step 4.
You need rules to internally rewrite the URI if the request was actually for a php or html file:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*)$ /$1.htm [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [L]
.htaccess
So all in all, you should have these rules in your htaccess (ditch any rules you may already have trying to solve this issue):
# Make sure Multiviews is off
Options -Multiviews
# turn on rewrite engine
RewriteEngine On
# Redirect requests that are missing www and not a subdomain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# Redirect if requests is for a .htm, .html, or .php
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.(php|html?)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%2 [L,R=301]
# rewrite to the proper extension if such a file exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*)$ /$1.htm [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [L]
So altogether:
if browser URL address bar says: http://example.com/index.html it will be redirected to http://www.example.com/index
if browser URL address bar says: http://www.example.com/index it will be shown the content at http://www.example.com/index.html (or index.php if that exists). Address bar remains unchanged.
if browser URL address bar says: http://foo.example.com/images/image.png you will see the image at /images/image.png. Address bar remains unchanged.
if browser URL address bar says: http://foo.example.com/path/foo/bar.php it will be redirected to http://foo.example.com/path/foo/bar
if browser URL address bar says: http://foo.example.com/path/foo/bar it will be shown the content at http://foo.example.com/path/foo/bar.php. Address bar remains unchanged.
Remove .html:
RewriteRule ^(.*).html $1 [L,QSA]
Add www.:
RewriteCond % ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
On which hosting is located your website ? Are you sure mod-rewrite is enabled ? Did you contact your host to put it on, if not activated ?
Solution previously posted by androbin (https://stackoverflow.com/a/12553613/1713771) is correct.
The .htaccess file that I use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
</IfModule>
Which removes the .html file extension and forces www.

Simple URL Rewrite Problems

I'm having trouble re-writing a simple URL with .htaccess.
The URL I am trying to rewrite is:
http://www.domain.com/index.php?page=PAGE_NAME
I would like the PAGE_NAME to be directly after the domain, for example if PAGE_NAME is blog:
http://www.domain.com/blog
At the moment I have tried the following with no success:
RewriteRule ^/?([a-zA-Z0-9_-]+)?$ index.php?page=$1
All help is really appreciated.
Thanks in advance.
My current .htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
ErrorDocument 404 /search.php?page=notfound
# Add WWW to URL
RewriteCond %{HTTP_HOST} ^cristianrgreco\.com$ [NC]
RewriteRule ^(.*)$ http://www.cristianrgreco.com/$1 [L,R=301]
# Remove trailing slashes from end of URL
RewriteCond %{HTTP_HOST} !^\.cristianrgreco\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]
# Rewrite main page URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+) [NC]
RewriteRule ^ %1? [L,R=301]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,NC]
# Rewrite download URLs
RewriteRule ^download/([a-zA-Z0-9._-]+)/?$ download.php?file=$1
# Rewrite page navigation links
RewriteRule ^(.+?)/page-([a-zA-Z0-9_-]+)?$ $1.php?currentpage=$2
# Rewrite article URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ articles.php?article=$2
# Remove file extension from PHP files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L,QSA]
</IfModule>
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#add these next 2 lines if you need to redirect http://www.domain.com/index.php?page=PAGE_NAME to http://www.domain.com/PAGE_NAME
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+) [NC]
RewriteRule ^ %1? [L,R=301]
#send request to index.php
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,NC]