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

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.

Related

How to allow only an IP, Google Structured Data Testing Tool, and the W3C Markup Validation Service to access a website, while it's being developed?

I'm trying to develop, test, and debug a website's CSS, and PHP code, and block the public from seeing any of the websites pages (by redirecting them to a parked "maintenance" page), while the site is still being developed. This site is NOT a WordPress site, so the problem is NOT solvable by simply applying a plugin. However it may be solvable by using .htaccess, if it's configured correctly.
I'm using the following code in the.htaccess file in the root of my websites tree:
<IfModule mod_rewrite.c>
# Allow the developer(s) (set to example.com's IP address for demonstration purposes) access to the website, via their IP address. #
RewriteCond %{REMOTE_ADDR} !^(93\.184\.216\.34)
# Allow users access to the directory containing the pages that handle HTTP error codes. #
RewriteCond %{REQUEST_URI} !^.*\/(error_documents)\/.+$ [NC]
# Allow users access to the parked "maintenance" page (duh!). #
RewriteCond %{REQUEST_URI} !^.*\/park\/index\.php$ [NC]
# During development and maintenance periods, redirect all others to the parked "maintenance" page. #
RewriteRule .* /park/index.php [R=302,L]
</IfModule>
Now, this works perfectly fine for it's intended purpose, but now I have (2) new problems:
It also blocks the Google Structured Data Testing Tool, which I need to give access to for testing the websites "Structured Data".
It also blocks the W3C Markup Validation Service, which I need to give access to, to check the markup's validity.
In order to solve these (2) problems, the following has to be accomplished:
Figure out the HTTP_USER_AGENT string the aforementioned (2) services are using, to access the website, and specifically allow those user agents (i.e. In consistency with the .htaccess code block, above, check that the request is NOT coming from those (2) services. If it is, then be sure NOT to redirect them to the parked "maintenance" page).
To give you an initial starting-point clue, that directive might look a little something like this:
RewriteCond %{HTTP_USER_AGENT} !^GoogSDTT
RewriteCond %{HTTP_USER_AGENT} !^W3CValidator
I found the answer to my question (with a little bit of online research):
With help from this page, and this page, from whatismybrowser.com, I was able to come up with the following .htaccess directive:
<IfModule mod_rewrite.c>
# Allow the developer(s) (set to example.com's IP address for demonstration purposes) access to the website, via their IP address. #
RewriteCond %{REMOTE_ADDR} !^(93\.184\.216\.34)
# Allow access from [ Google Structured Data Testing Tool ]. #
RewriteCond %{HTTP_USER_AGENT} !^.*(Google\-Structured\-Data\-Testing\-Tool).*$ [NC]
# Allow access from [ W3C Markup Validation Service ]. #
RewriteCond %{HTTP_USER_AGENT} !^.*(W3C_Validator|Validator\.nu\/LV).*$ [NC]
# Allow access from [ W3C CSS Validation Service ]. #
RewriteCond %{HTTP_USER_AGENT} !^.*(W3C_CSS_Validator).*$ [NC]
# Allow users access to the directory containing the pages that handle HTTP error codes. #
RewriteCond %{REQUEST_URI} !^\/(error_documents)\/.*$ [NC]
# Allow users access to the parked "maintenance" page (duh!). #
RewriteCond %{REQUEST_URI} !^\/park\/index\.php$ [NC]
# During development and maintenance periods, redirect all others to the parked "maintenance" page. #
RewriteRule .* /park/index.php [R=302,L]
</IfModule>
Please, let me know if I missed something, or if there is an error in my code.
Regards,
PS: And, no, I’m not worrying about someone spoofing the HTTP_USER_AGENT string, because this is meant to keep “normal” people from viewing the site. Others would eventually find some way to hack their way in, regardless of how many preventative security measures I take, and no matter how proactive I try to be. My father always warned me, “A thief can steal the salt out of a biscuit, and not even break the crust,
” and “Locks are only for honest people,” So I took heed to his advise, and don't worry so much about hackers anymore. Besides, even if they DO get in, what's the worst that can happen? They see a broken, or unfinished website?

Strange 301 redirect needed .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]

Redirect scam domain to warning?

A client of mine has found out today that someone has set up a spoof domain (1 letter different - design instead of designs) and has tried to set up credit accounts in his name.
The domain will undoubtedly be removed, but is it possible in the meantime using some htaccess trick to check if a visitor to www.designs.com has been referred from www.design.com and if they have, send them to a warning page?
You can use this code in your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http(s)?://([^\.]+\.)?design\.com
RewriteRule ^.* /warning.html [L,R=302]
It will redirect any pages coming from design.com and its possible subdomains (using http or https) to a page named warning.html at the root of your project.
You can test this rule with this online tool.

Website self-referral

Google Analytics is showing my own root domain is the referral for a large portion of my website's traffic.
So, mywebsite.com is showing as the referrer for www.mywebsite.com.
I have tried adding a referral exclusion within Analytics with no success. I have cleaned my .htaccess which also hasn't been successful
.htaccess code
RewriteEngine On
Options +FollowSymLinks
# Redirects mywebsite/uk/anypage to mywebsite.eu/anypage
RewriteRule ^uk/(.*)$ /$1 [NC,R=302,NE,L]
# Redirect 404 to index
ErrorDocument 404 /
# Use www version
RewriteCond %{HTTP_HOST} ^mywebsite.eu [NC]
RewriteRule ^(.*)$ http://www.mywebsite.eu/$1 [L,R=301,NC]
This is what it looks like in Analytics:
I've put a lot of time into trying to find the cause so any suggestions, .htaccess related or otherwise, are very welcome
Did you migrate recently to Universal Analytics? It migrates the previous utmz cookie values that stored traffic source, so if your users had self-referrals prior to upgrading to UA, this will persist int your data. Could you check if there are new sessions in this traffic?
Make sure to add to the referral exclusion list your domain with and without the www.
For some reason certain sites it likes to have it while on others it does not, I would definitely try this and then see if the future sessions are any more clean before changing too much with the .htaccess.

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

Resources