Rewrite url issue - .htaccess

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

Related

HT Access issue

I have a problem with our site after reinstalling the SSL certificate, where the www version of the site (www.mountainx.com) pulls up a completely different site. if you enter the non-www version (mountainx.com), it goes to the correct website. Some of the rules in my .HTAccess file are here - Any help would be appreciated or at least I would like to get a discussion going -
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Also I have a certain number of proxy redirects that are not working either after the SSL change and I had to redirect them to the original hosts. To clarify, we use a third party to host our calendar but want to obscure the URL so it looks like we are still hosting the files. When the SSL certificate was installed, it suddenly started throwing up a 503 error so we agreed to change to the original URL hosted remotely. Also any good tutorials on .htaccess file out there? They seem to be pretty few and far between.

.htaccess redirect but show different URL

I want my site to act like this. If user inputs site.cz it should be redirected to site.eu/?lang=cs but the user should still see site.cz. Right now I have the following htaccess file:
RewriteCond %{HTTP_HOST} site\.cz [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://site.eu/?lang=cs [L,R=301]
which works great and redirects only .cz request (dont do anything with .eu requests) but it displays .eu/?lang=cs in final. Problem is that I dont only want to display site.cz all the time but also links like site.eu/folder1/file1/?lang=cs should be redirected to site.cz/folder1/file1/
Any ideas how to accomplish this?
Thanks in forward
Are you using XAMPP or a web host? Upon testing, URL rewriting isn't working in XAMPP.. Anyway, you can try to check this, just make sure that your two sites are hosted by the same web hosting account:
RewriteCond %{HTTP_HOST} site\.cz [NC]
RewriteRule ^(.*)$ http://site.eu/$1?lang=cs [P,L]
But it looks like your sites aren't hosted by the same hosting account as what you've said:
when I use [P] flag I get an error "Following URL was not found on this server" – horin

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.

URL Rewrite seems to be functioning like a URL Redirect

This is the rewrite section of my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^komarketingassociates\.com$ [NC]
RewriteRule ^(.*)$ http://www.komarketingassociates.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
RewriteRule ^industry-news/.*[$]([0-9]+)\.htm$ http://www.komarketingassociates.com/industry-news/article.php?articleid=$1 [L]
The last rewrite rule is meant to process requests for our repository of news articles. Until maybe a week ago it was working perfectly translating the SEO friendly link titles like this one, companies-are-becoming-more-accustomed-to-leveraging-social-media$715.htm into the proper format to retrieve them from the database. Recently, however, without any changes being made to the .htaccess file (to my knowledge) the links now seem to use this redirect to article.php?articleid=715 rather than doing it server side. The links on the site are still the seo friendly urls, but now it seems as if you are being redirected to the article template page.
We are using GoDaddy to host our site. PHP version 5. I am completely stumped as to how this is happening and how to fix it. Any help is appreciated!
Thanks!
-Bill
Proxy flag. (P)
Warning, will mess with IPs. Every single IP will look like it's your servers. However, mod_rewrite sets the X-FORWARDED-FOR when you use P, so you can extract (I'd recommend validating the IP as being real as well before trying to use it in any SQL) and use the IP address still.

How can I make my website redirect to another based on a cookie with .htaccess?

I'm trying to do this with .htaccess. So what I did was set a cookie named "domain" with the website I'm redirecting to. In my .htaccess file I have this
RewriteEngine on
RewriteCond %{HTTP_COOKIE} domain=([^;]+) [NC]
RewriteRule ^(.*)$ %1/$1 [L]
I got that code from a Google search. It's supposed to reditect http://mywebsite.com/something to http://someotherwebsite.com/something. But it's not working. Any tips?
Edit: I still haven't been able to get it to work. A temporary solution I'm using is using PHP to get the cookie value and redirect that way.
I would turn on RewriteLog on a fairly high level and see whether that provides a clue.

Resources