Force http on two pages using the .htaccess - .htaccess

I would like to redirect to pages from HTTPS to HTTP using the .htaccess file.
I have added the code for one page but when I add it for page2 it gives the site a redirect error.
Here is my code that works for one page going from HTTPS to HTTP in the .htaccess and I also make sure the .htaccess is in that directory with the files I want to be redirected as well.
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/page1.php$ [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
RewriteCond %{SERVER_PORT} !80
RewriteCond %{REQUEST_URI} ^/page1.php$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [R,L]
But when I add it like this below it doesn't work and I get a redirect error.
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/page1.php$ [NC]
RewriteCond %{REQUEST_URI} !^/page2.php$ [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
RewriteCond %{SERVER_PORT} !80
RewriteCond %{REQUEST_URI} ^/page1.php$ [NC]
RewriteCond %{REQUEST_URI} ^/page2.php$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [R,L]
I would like the pages to go to HTTP only using the .htaccess file without error:
http://mywebsite.com/page1.php http://mywebsite.com/page2.php

I figured it out. The correct code I needed is below:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/(page2.php$|page1.php$) [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
RewriteCond %{SERVER_PORT} !80
RewriteCond %{REQUEST_URI} ^/(page2.php$|page1.php$) [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [R,L]

Related

Redirect www to non www with subfolder and links

Ok, so i want to redirect all www to non www link,
I want to redirect www.example.com to example.com, and i can do that too using this code.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
But when someone try to access www.example.com/example, that redirects the site to example.com,
I want a code that can redirect www.example.com/example or any subfolder to example.com/example or any subfolder.
Kinda like a wildcard entry, how to do that?
Also if possible force ssl on it too.
Thank You.
If I've understood your question correctly:
www.example.com/dir/anypage.html to example.com/dir/anypage.html (same page)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [nocase]
RewriteRule ^(.*) http://example.com/$1 [last,redirect=301]
OR Use:
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/subfoldername/$1 [R=301,L]
-------- Alternative---------
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1 [L]
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(/)?$ /subfolder/index.php [L]
To force SSL:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

Forcing https only on one page of a yii website

I am trying to force https only on one page of a yii framework website, and let the rest of the pages be http. The page that would be forced https is
https://www.mywebsite.com/index.php?r=user/profile
When I do the following, it forces https on all the pages on the website.
RewriteEngine On
# Go to https
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/index.php?r=user/profile$ [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
# Go to http
RewriteCond %{SERVER_PORT} !80
RewriteCond %{REQUEST_URI} !^/index.php?r=user/profile$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R,L]
The query string isn't part of the %{REQUEST_URI} variable, try:
RewriteEngine On
# Go to https
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} ^r=user/profile$ [NC]
RewriteRule ^(index\.php)$ https://www.mywebsite.com/$1 [R,L]
# Go to http
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_REFERER} !/index\.php\?r=user/profile
RewriteCond %{REQUEST_URI} !^/index\.php$ [OR]
RewriteCond %{QUERY_STRING} !^r=user/profile$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R,L]

.htaccess or between rewrite rules

I have an issue with my .htaccess redirects.
What i want to happen is that all requests to the root, index.php and item.php are redirected to their http equivalent, but every single other page is directed to it's https version.
This is to be used to prevent the slower loading of pages.
Here's what's in my htaccess at the moment:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC] .
RewriteCond %{REQUEST_URI} !^/(index.php|item.php)
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC] .
RewriteCond %{REQUEST_URI} ^/(index.php|item.php)
RewriteRule ^(.*)$ http://www.domain.com/$1 [R,L]
Ideally i need to have an OR inbetween the 2nd and third lines of each collective statement! But i can't work out how to do this!
use [OR] at the end of the 2nd statement.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC,OR]

Forcing http on homepage using .htaccess

I'm using .htaccess to force https on certain pages and http on other pages and it's working fine. But I need to force http on home page (example: http://website.com) and I don't know how to do that. I tried RewriteCond %{REQUEST_URI} ^/index.php/? but as I'm using drupal that didn't work.
This is the script I'm using:
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/register/? [OR]
RewriteCond %{REQUEST_URI} ^/admin/?
RewriteRule ^(.*)$ https://website.com/$1 [R,L]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} ^/about-us/? [OR]
RewriteCond %{REQUEST_URI} ^/help/?
RewriteRule ^(.*)$ http://website.com/$1 [R,L]
Any help is appreciated
First, instead of RewriteCond %{SERVER_PORT}, you can use RewriteCond %{HTTPS}, which should be more reliable in detecting HTTPS.
The homepage has the RewriteRule pattern ^$. So the rule would be
RewriteCond %{HTTPS} =on
RewriteRule ^$ http://website.com [R,L]
When everything works as you expect, you can change R to R=301.

Force SSL only on certain pages with .htacces

I'm trying to force ssl on certain pages, but also on a directory. But I am getting a page loop error. This is my htaccess file:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} dashboard
RewriteRule ^(.*)$ https://www.example.com/dashboard/$1 [R,L]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_FILENAME} get_started_lp.php$
RewriteCond %{REQUEST_FILENAME} get_started.php$
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Basically is someone goes to get_stared_lp or get_started or the dashboard I want SSL. Any suggestions?
Never-mind. It ended up being a issue with how Rackspace handles their SSL. For more information:
http://www.wreckedmagazine.com/jasonsmall/2010/09/14/force-ssl-on-a-rackspace-folder/

Resources