Redirect One Directory from https to http - apache

For some reason, I'm seeing one directory's contents showing up on Google with https URLs. Since this breaks some page elements, I need to check for that particular directory for https requests and redirect to http. Https is used within several other directories, so I don't want to make a blanket redirect.

Use that in your root .htaccess:
RewriteEngine on
RewriteCond %{HTTPS} on
RewriteRule ^particularDirectory/? http://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
Or that in your particularDirectory/.htaccess:
RewriteEngine on
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]

There are many sample script for this. And if you understand rewrite rules it's not that hard. I found this one at ServerFault that should work for you. Don't forget to alter it for you specific directory (if you don't want everything to be rewritten to https).
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]

Related

mod_rewrite - HTTP to HTTPS for some pages keeping pre-existing rewriting rules

I have a .htaccess file with a few rules that have served well so far. Now I implemented SSL in the website and I need to add a rule to redirect just a single webpage to HTTPS, leaving the rest as it was. Unfortunately but I'm not being able to do so.
Pre SSL code
RewriteEngine On
RewriteBase /
#Redirect http://example.com to http://www.example.com
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
#Rewrite http://www.example.com/123 to http://www.example.com/data.php?id=123
RewriteRule ^([0-9]+)$ data.php?id=$1 [L,NC]
#Rewrite http://www.example.com/about to http://www.example.com/about.php
RewriteRule ^about$ about.php [L,NC]
#Rewrite http://www.example.com/ab123 to http://www.example.com/abdisp.php?id=123
RewriteRule ^ab([0-9]+)$ abdisp.php?id=$1 [L,NC]
Now I need to redirect to HTTPS only the requests for a single specific script. The pre SSL rules need to keep working as they did so far, so all I do is add the following code:
New HTTP to HTTPS
#If HTTPS is not set and the script being requested is rvservice.php, redirect to HTTPS
RewriteCond %{HTTPS} != on
RewriteCond %{REQUEST_URI} rvservice\.php
RewriteRule ^/rvservice\.php(.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This is not working. When I upload this addition to the original code into the server, I get internal server error. I've tried putting this new code at the beginning of the original .htaccess file, but the result is the same. I re-wrote it as a single rewrite rule (no conditions) but I get a redirect loop error. I've been reading posts on SO about HTTP to HTTPS and applied their solutions, but it seems there is something in my old code that does not get along with the new lines. I feel I'm close to the solution, but for the time being it looks I'm closer to clueless. I'll appreciate any pointers in the right direction. Thanks!
In .htaccess, no / at the beginning in test RewriteRule parameter :
RewriteCond %{HTTPS} off
RewriteRule ^rvservice\.php(.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But if you use %{REQUEST_URI} they always start with a /

Redirect HTTPS to HTP on Apache

I have an account with a webhost that uses Apache servers. The webhost's file structure uses subfolders for secondary domains of the primary account domain.
What do I need to add to this .htaccess file to redirect if someone types https:mysubdomain in the browser URL. I want to redirect from https to http, ie. http:mysubdomain.
RewriteEngine on
# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
RewriteCond %{HTTP_HOST} ^myseconddomain\.myprimarydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.myseconddomain\.myprimarydomain\.com$
RewriteRule ^/?$ "http\:\/\/mysedonddomain\.com" [R=301,L]
Edit Update:
Thank you for suggestions. The approach of modifying the .htaccess file for the subdomain in the subfolder didn't work, even after clearing browser cache. What about modifying the .htaccess for the maindomain. I tried this but it didn't work either. Maybe my syntax?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^https:\/\/myseconddomain.com$
RewriteRule ^www.myseonddomain.com/ [R=301,L]
I have spoken at length with the webhost, Hostmonster, and all they could tell me was that the SSL certificate was working "correctly" - even thought it is associating with unrelated domain names that are not supposed to have any certificate. I guess that is what User82217 was saying, there is no other way than to purchase a wildcard SSL?
Edit Update: I tried putting this in the .htaccess of the maindomain and the seconddomain and nothing works to redirect from https to http when the user types https:// in front of mysecondubdomain.com in the URL
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^https
RewriteRule ^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Anybody got any more ideas? Thank you.
To force HTTPs to HTTP then you can use the following in your .htaccess file:
#Force HTTP on everything
RewriteCond %{HTTPS} =on
RewriteRule ^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
You didn't specifiy if you wanted to remove www or not, but on the assumption that you do, you can also remove that by including the following rule:
RewriteCond %{HTTP_HOST} ^www\. [OR]
Therefore checking if www is in the URL or not, so altogether using:
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} =on
RewriteRule ^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Make sure you clear your cache before testing this.

Redirecting from http to https using htaccess except for certain subdirectories

I recently learned that Godaddy provides a code for their customers who purchased their SSL certificate to redirect http to https using htaccess file. The code is shown below:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
However, there are about 10 subdirectories that I do not wish to redirect. What would be the most efficient way to exclude those directories?
Also, is the code above compatible with Cloudflare's CDN? I learned that some loops might occur. Thanks
You should be able use this, just place the directories that you do not want to be HTTPs inside the brackets:
RewriteEngine On
#Force everything to HTTPs
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
#Checks if HTTPs is on for foobar & foobar2, if it is, turn it off
RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(foobar|foobar2)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
You can obviously add as many directories as you like to this, just separate them with |.

I want to use.htacess to re-direct only one file

There's a file that I have that needs to be accessible to an outside source via http. My whole site is https. If I do the following:
# Redirect all http traffic to https, only if it is pointed at specialfile.txt
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/specialfile.txt/
RewriteRule ^(.*)$ http://example.com/specialfile.txt$1 [R=301,L]
Does it look like this would work. I'm an absolute noob when it comes to Apache re-write rules, and I would RTFM but I need this change pretty quick.
I should also mention that there is already
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
in the .htaccess file along with some Wordpress stuff.
If you put something like the following in the root folder .htaccess file, it ought to work:
RewriteEngine on
RewriteCond %{REQUEST_URI} specialfile.txt$
RewriteRule ^(.+)$ http://example.com/$1 [R=301,L]
The directive translates as:
If the URI requested ends in specialfile.txt
Capture all characters (from one to any number) after https://domain.tld/
Rewrite the URI as http://domain.tld/all-captured-characters

apache mod rewrite for subdomain

On my webserver, I have one website located at example.com/site (in /var/www/site).
I want it to be accessed through site.example.com, and I'm aware I need to use mod-rewrite to enable this.
Is there a concise snippet I can use to do this? I need to make it extensible so other sites can be accessed this way as well. Thanks in advance.
If this is a matter of one specific subdomain: site, then you should explicitly rewrite just that one subdomain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^(.*)$ /site/$1 [L]
The second condition prevents an internal rewrite loop and making your URI look like /site/site/site/site/site/site/ etc...
This would go in your .htaccess file in your document root, /var/www/
If you really want to arbitrarily redirect any subdomain, you can try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]*)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)[^:]*:\1 [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
Here, the second condition serves the same purpose, it tries to make sure the request URI doesn't start with the subdomain match from the host. See https://stackoverflow.com/a/11508430/851273 for a better explanation.
First you need to configure your server to accept any subdomain for your domain example.com and redirect it to your virtual host that as well has to accept any subdomain. After that, you can use the following rule to rewrite that subdomain internally to a folder with the same name:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ %1%{REQUEST_URI} [L]