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

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

Related

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 |.

Redirect One Directory from https to http

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]

How can I change all subdomains to read different content from their respective subdirectories?

I have my DNS configured to accept any subdomain (wildcard *), but I am having trouble feeding back the required content to the browsers.
I would like each subdomain to return the relative content, which resides in subdirectories of the same name within the public_html path of my server.
eg, example.domain.com/picture.jpg would actually request the file at public_html/example/picture.jpg
Currently I have tested the followed .htaccess code, but it is not functional:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ %1/$1 [L]
This code, and similar tests, can redirect based on the subdomain (%1) fine, but the request string ($1) seems to be the issue.
Maybe you could take a look at the mod_vhost_alias module :
http://httpd.apache.org/docs/2.0/mod/mod_vhost_alias.html
Try the following:
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+).domain.com [NC]
RewriteRule ^/(.*) http://domain.com/%1/$1 [L]
I didn't test this one but I use similar rules for proxying:
RewriteCond %{HTTP_HOST} !^domain.local [NC]
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+).domain.com [NC]
RewriteRule ^/(.*) http://domain.local/%1/$1 [P]

How do I use mod_rewrite to alter which file is fetched based on the URL's sub-domain?

So say I have a URL being requested:
sub.domain/file.bin
and I wanted this file to be fetched:
domain/hosted/sub/file.bin
How would I structure the rewrite directives? I've been working on this for days, and it just gives me a headache...
The sub-domains are numerous and are added and removed often, so I'm trying to find a solution that works for all of them.
In sub.domain's httpd config:
RewriteCond %{HTTP_HOST} ([^\.]+).domain
RewriteRule ^/file.bin$ http://domain/hosted/%1/file.bin
Assuming the home directory for www.domain.com is domain/ and that - given any subdomain sub.domain.com - you want the files for the home directories for that sub-domain to be in domain/hosted/sub/
Try something like this:
RewriteEngine on
// If the host is just mydomain.com, do nothing more
// this is to prevent some recursion problems I've read of...
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^.*$ - [L]
// Otherwise strip off everything before mydomain
// And add it to the start of the request
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?mydomain\.com$ [NC]
RewriteRule ^.*$ %1%{REQUEST_URI} [QSA]
// Then prefix with 'hosted'
RewriteRule ^(.*)$ hosted/$1 [QSA,L]
You may also need a wildcard entry in your DNS or something... but I will admit DNS and htaccess mod rewrite are some of my weaker points. See also http://www.webmasterworld.com/forum92/138.htm
Try this rule:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %1 !=www
RewriteRule !^hosted/ hosted/%1%{REQUEST_URI} [L]