Strange 301 redirect needed .htaccess - .htaccess

I have a website that has recently been getting a bit busy. I put out a free demo version of an app I made that got a lot of downloads. A 600kb file generated 110GB of traffic per day after about 1 day. So I successfully made a redirect to another server for all files with htaccess like this.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain.mydomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.subdomain.mydomain.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/myfolder/$1 [L,R=301,NC]
So that is fine and my server is much less busy. But I make a machine that gets art and backgrounds for various screens off the server as well so that I can add new images whenever I want and the machines will all download/cache/display them. I am doing this with just directory listing enabled. They are all jpg. I anticipate a surge in traffic for this soon as well so I want to store the images in an amazon aws S3 bucket. I can access the images no problem using direct linking. But I need my machines to see a directory listing which I found out amazon doesn't really do. I have directory listing working with a script but it only seems to work in a computer web browser. That brings me to my strange request.
Is there a way to keep all my customer machines getting directory listing from hxxp://mydomain.com/folder but the 301 redirect only kicking in when a jpg is downloaded?
So the machines see the listing off the old server but when a file is downloaded
1.jpg = new host
2.jpg = new host
Thank you in advance.

You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?subdomain.mydomain.com [NC]
RewriteRule ^(.+\.jpe?g)$ http://newdomain.com/myfolder/$1 [L,R=301,NC]

Related

How to prevent downloading using htaccess

I have some video files few of which are not public. I want to access them through video player but I also need to prevent the direct download from the address bar. Also if a user pays for it they can download.
I tried in .htaccess file but still not solved.
I tried the following:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^(https?://)?yoursite.com
RewriteRule ^/?video_files - [L,F]

Where do I upload an .htaccess file on a shared hosting account?

I am a novice with server side coding so bear with me if the answer is obvious to some:
I designed the front end of a website sometime ago. It resides in a shared hosting environment as a subdirectory. I am trying to upload an .htaccess file in order to redirect to its mobile version ( that I did not design and that is hosted on another account). I have tried uploading the .htaccess file to both the root directory and the subdirectory and each have their issues.
When I upload to the root the redirect does not occur on any devices. It was not my wish but I was expecting that all websites under the root would be redirected to the mobile version of this one site but nothing occurred.
When I upload the .htaccess file to the subdirectory - viewing from all devices returns "The page you requested is NOT AVAILABLE"
The following code is what I was provided by the mobile designers to input into the .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
#Check if this is the desktop to mobile query string
RewriteCond %{QUERY_STRING} (|&)m=1(&|$)
#Set a cookie, and skip the next 2 rules
RewriteRule – [CO=mredir:1:%{HTTP_HOST},S=2]
#Check if this is the mobile to desktop query string
RewriteCond %{QUERY_STRING} (|&)m=0(&|$)
#Set a cookie, and skip the next rule
RewriteRule – [CO=mredir:0:%{HTTP_HOST},S]
RewriteCond %{HTTP_USER_AGENT}
“android|blackberry|iphone|ipod|iemobile|opera
mobile|palmos|webos|googlebot-mobile” [NC]
#Check if we’re not already on the mobile site
RewriteCond %{HTTP_HOST} !m\.
#Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(|&)m=0(&|$)
#Check to make sure we haven’t set the cookie before
RewriteCond %{HTTP_COOKIE} !^.mredir=0.$ [NC]
#allow mobile sites to access images on the main site
RewriteCond %{REQUEST_URI} !\.(gif|png|jpe?g|ico)$ [NC]
RewriteRule ^(.*)$ http://example.mobi/ [L,R=301]
</IfModule>
I have ommitted the url that would replace http://example.mobi
I cannot detect if there are any syntax errors in the above code, any help would be greatly appreciated.
Thanks
It's just my guessing. But why do you urge to use .htaccess for simple redirects? If only one site (particular subdirectory on you root / hosting) should always redirect to new, mobile version, then why can't you set a permanent redirect using your server / hosting configuration?
Such tools are in nearly every control panel like cPanel, DirectAdmin etc. Even if your hosting provider runs its own, sophisticated piece of software for running and managing hosting server, I can hardly believe it that it not provide you with any tool or solution for making permanent redirects and thus forcing you to play this game with .htaccess.
BTW: If you 100% sure that you can't do this via server / hosting configuration, and .htaccess is the only way, than maybe this is the right moment to change your hosting? After all, domain / address / URL is the most important for each project, and it can be hosted on any hosting.
As for your problem, I would advice to run second scenario (.htaccess in subfolder) from a regular PC or Linux and via browser equipped with FireBug or simillar developers tool. Then enter your webpage that should do the redirect, but fails, and carefully examine that tool's log / path to see, where you're actually redirected? Is this trully URL you've been expecting? Most times "The page you requested is NOT AVAILABLE" means what it means, that is -- a website tried to redirect your browser to an non-existing URL.

Rewrite url issue

I'm hoping someone can help me. For my website I have a corresponding mobile site that has the same content as my full site but display it for mobile devices. Basically I want to send all requests from the full site to the mobile site unless the url variable sms exists
So in my htaccess file for my full site I have this:
RewriteCond %{QUERY_STRING} !sms=1 [NC]
RewriteRule ^(.*) http://mobile.mysite.co.uk/$1 [QSA,NC]
But when I got to www.mysite.co.uk/news/index.cfm&sms I get the following ColdFusion error for the full site:
File not found: /news/index.cfm
With debugging turned on I've noticed that the CGI variable PATH_TRANSLATED has been changed from
C:\webistes\mysite\news\index.cfm
To
C:\JRun4\bin\http:\mobile.mysite.co.uk\news\index.cfm
I'm at a loss to undestand what's going on? Any help or insight would be greatly appreciated.
Additionally I'm running a multi server install of ColdFusion 8 and using Apache configured for ColdFusion.
It seems like you want to make an external redirection, but your RewriteRule currently only rewrites the URL internally. Try adding the R and L flags to your rule to see if that makes a difference:
# Stop and redirect immediately to the mobile site
RewriteCond %{QUERY_STRING} !sms=1 [NC]
RewriteCond %{HTTP_HOST} !^mobile
RewriteRule ^(.*) http://mobile.mysite.co.uk/$1 [QSA,NC,R,L]
I also added in a RewriteCond to make sure that it doesn't redirect you if you're already on the mobile site, in the event that both of your sites point to the same place (you can remove it if they don't; just wanted to save you the headache in the event that they did).

How to setup .htaccess to rewrite to a different folder

I'm moving my site to a new host but I need to have my current server continue to handle requests (not all files can be moved to the new server).
So I added a parked domain to my old server (old.mydomain.com) and I want all requests to it to be written to the files from the old site.
My old site (mydomain.com) was hosted internally in a folder (/public_html/mydomain/) and I want all requests to old.mydomain.com to be rewritten to the same folder.
So if mydomain.com/blog was internally at /public_html/mydomain/blog, I now want old.mydomain.com/blog also to reach /public_html/mydomain/blog.
Here is the .htaccess that I'm trying to use:
RewriteCond %{HTTP_HOST} ^old\.mydomain\.com/*
RewriteRule ^(.*)$ mydomain/$1 [NC,L]
But for some reason as soon as I add the $1 in the rewrite rule I get an internal error.
Any ideas?
Configure this as a separate vhost called old.mydomain.com and ensure it comes before *.mydomain.com in your vhost definitions (i.e. higher in vhosts.conf). Give old.mydomain.com the same DocumentRoot as your previous domain had.
.htaccess is the most processor intensive way to serve a webpage, and should only be used there are no other options available.
Please try to fix your .htaccess config as follows:
RewriteCond %{HTTP_HOST} ^old\.mydomain\.com$
RewriteRule ^(.*)$ /public_html/mydomain/$1 [NC,L]

Creating SubDomains to Absolute Paths with .htaccess

Hey, My host is absolutely terrible. For some odd reason creating a subdomain in cPanel simply does not work, and their support lines are always busy. I thought I could get around this by using .htaccess. I'm sure it's not that hard, but I'm kind of new to mod_rewrite and have had little success searching in the last 5 hours. Heres the situation:
/home/user/public_html automatically redirects to http://www.example.com
Since I'm using a CMS in public_html it has already added the rule in .htaccess to redirect anything unfamiliar after example.com/ to a 'Page Not Found'
/home/user/subdomain needs to redirect to http://subdomain.example.com
How should I go about creating a subdomain redirection to an absolute path? Or How can I add an exception in my .htaccess
I doubt you'll be able to get your subdomain to function outside of your public_html folder (although I'm no server admin). Typically that requires DNS modifications or tweaking the server's configuration. Have you tried making a sub-directory and rewriting calls to the subdomain? For example this placed in the .htaccess within your public_html directory:
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteRule (.*) /subdomain/$1 [L]
I'm not sure if that would work (never needed to test it myself), but it's more likely to function than trying to target files that live outside the directory specified by the webhost as the location of your domain's files.
Good luck!
Try this rule:
RewriteCond %{REQUEST_URI} !^/home/user/
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ /home/user/%1%{REQUEST_URI} [L]
But your webserver already needs to be configured so that every request of foobar.example.com gets redirected to this specific virtual host.

Resources