Htaccess redirections depending on OS - php

1- I need the .htaccess code for this condition If the user clicked on:
website1.download
OR
website2.download
It redirects the user depending on his OS for example:
If on PC, redirect to http://google.com/
If on Android, redirect to https://play.google.com/store
If on iOS, redirect to https://www.apple.com/ios/app-store/
2- I've more than one website in my host and I need to create a one .htaccess to all websites in the host, where I can create the .htaccess file?
That is the htaccess codes and it's not working:
<If "req('Host') = 'website1.download' && = 'website2.download'">
# turn on rewrite engine
RewriteEngine on
# only detect smart phone devices if we are not on mobile site
# to prevent redirect looping
RewriteCond %{HTTP_HOST} !^https://google.com/$
# Android
RewriteCond %{HTTP_USER_AGENT} "android" [NC]
# redirect to google play
RewriteRule .? https://play.google.com/store%{REQUEST_URI} [L,R=302]
# iOS
RewriteCond %{HTTP_USER_AGENT} "android" [NC]
# redirect to app store
RewriteRule .? https://www.apple.com/ios/app-store/%{REQUEST_URI} [L,R=302]
</If>

Okay, I'm going with example.com and example.net since that's what they are for. Also, I'm assuming these domains point to your server and they have all been bound to it, too. Lastly, I'm assuming you have a pattern already for device detection? It isn't super clear in your question. (If you don't you can probably just base this off Google and this thread.)
You can just stack RewriteCond and join them with an OR
RewriteCond %{HTTP_HOST} example.com [NC,OR]
RewriteCond %{HTTP_HOST} example.net [NC]
RewriteCond %{HTTP_USER_AGENT} "android" [NC]
RewriteRule .? https://play.google.com/store%{REQUEST_URI} [L,R=302]
Technically that is a regex, too, so some care should be taken for special characters as well as being more specific, adding subdomain support and just merging everything into a single condition.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.(com|net)$ [NC]
RewriteCond %{HTTP_USER_AGENT} "android" [NC]
RewriteRule .? https://play.google.com/store%{REQUEST_URI} [L,R=302]
There's HTTP_HOST vs SERVER_NAME that you should be aware of, too.
All that said, I can't think of a good reason to do this. From an SEO-perspective, you are going to wind up hiding 2 out of 3 redirects because spiders store information they find at a given URL, and if that changes, the most recent change wins. From a user-perspective, people are mostly trained to click the badges from the stores, I wouldn't introduce something new and weird. If you are trying to track a click, I'd just use JS.

Related

Rewrite live site front-end to subfolder (but not /admin) and nothing on localhost

Lots of info nearly solves this, but nothing will quite crack it yet.
The site has a front-end (all in root basically), and an admin panel living in /admin. Basic stuff.
The site runs remotely on http://www.example.com and locally on http://foo
I want nothing locally redirected at all.
On the live server I just want front-end traffic redirected to a sub-folder /coming_soon but no redirection on the admin panel. So the client can start work in admin, but the public will only ever see the content in /coming_soon. (Plus I guess the admin login page, but that's fine).
Closest I came was:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !=foo
RewriteRule ^$ /coming_soon [L]
</IfModule>
But that let me hit the "real" front-end by browsing directly to http://www.example.com/index.php
Your help much appreciated.
Hopefully I got your question right^^ Wasn't sure about the /admin part, who should access or if possible no one or...But the following is my take on your problem:
RewriteEngine On
#excludes HOST=foo and URI=/admin from rewrite to /coming_soon
RewriteCond %{HTTP_HOST} !^foo [OR]
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^ http://www.example.com/coming_soon [R=301,L]
You can additionally set a location-directive and only allow entering /admin from specific IP(s).
<location /admin>
required ip 10.11.12.13
required ip 20.30.40.0/24
</location>
update
RewriteCond %{HTTP_HOST} !=foo
RewriteCond %{REQUEST_URI} !/(coming_soon|admin)
RewriteRule ^(.*)$ /coming_soon [R=302,L]
And R=302 is just temporary rewrite = won't be cached by browsers with target location. R=301 would tell browsers to save the target /coming_soon right away.
...if any one can improve this, or make it more elegant, I'd love to hear!
RewriteCond %{HTTP_HOST} !=foo
RewriteCond %{REQUEST_URI} !/coming_soon
RewriteCond %{REQUEST_URI} !/admin
RewriteRule ^(.*)$ /coming_soon [R=302,R]

Disable a PHP website temporarily

I have a PHP website that I own. I want to temporarily disable the website while I am making my changes (there are a lot to do). I tried to rename the index.php file but the end-user can always navigate to a page by typing the URL ( or if he had bookmarked the page ). Is there a way I can disable the whole website temporarily?
Edit : This accepted answers work for an apache web server. What I am using presently is IIS6 ( and not IIS7 where the same rewrite can be done in web.config file ). Is there a way around for this problem in IIS6?
You can use .htaccess file to redirect to maintenance page:
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ http://domain.com/maintenance.html [R=307,L]
Some useful links:
http://www.shellhacks.com/en/Redirect-Site-to-Maintenance-Page-using-Apache-and-HTAccess
http://perishablepress.com/htaccess-redirect-maintenance-page-site-updates/
http://wp-mix.com/maintenance-mode-htaccess/
While bartek beat me to an Apache mod_rewrite rule that would force the site to redirect all traffic to a maintenance/offline page, I wanted to post my variation on the idea which can allow a specific IP address to access the site:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^123.45.67.890$
RewriteCond %{REMOTE_ADDR} !^127.0.0.1$
RewriteCond %{REMOTE_ADDR} !^::1$
RewriteCond %{REQUEST_URI} !^/offline.html$
RewriteRule ^(.*)$ http://your.great.website/offline.html [R=302,L]
Just a note that depending on your version of Apache, you might have to escape the . and : in the IP addresses like this:
RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.890$
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
RewriteCond %{REMOTE_ADDR} !^\:\:1$
The idea is if the visitor is not coming from the IP address 123.45.67.890—or the localhost addresses of 127.0.0.1 (IPv4) or ::1 (IPv6)—and the URI requested is not /offline.html then redirect that person to http://your.great.website/offline.html.
Which means you should just replace 123.45.67.890 with the IP address you would be connecting from to allow you to have a window into the website while you perform work. But anyone else who is not 123.45.67.890? Well, they see the offline.html page.
I would also propose this solution.
RewriteEngine On
RewriteCond %{QUERY_STRING} !access=dbcaf771cc0c4e23a0fc895d0afa106f
RewriteCond %{REQUEST_URI} !^/maintenance.html$
RewriteRule ^(.*)$ /maintenance.html [R=302,L]
The idea is unless your request contains access=dbcaf771cc0c4e23a0fc895d0afa106f within the query string, you'll simply get redirected to the maintenance page.

Need helping on htaccess redirect [duplicate]

Hello Folks at Stackoverflow,
I'm looking to redirect desktop users away from mobile site with .htaccess:
I already have this code which works perfectly when a mobile user tries to access the mobile version of my website example: m.website.com.
RewriteEngine On
RewriteCond %{QUERY_STRING} !^desktop
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|iphone|ipod|#opera mobile|palmos|webos" [NC]
RewriteRule ^$ http://m.website.com [L,R=302]
However if a user types m.website.com on a desktop browser, it goes directly to the mobile version content.
Is there a way to add any additional code to the .htaccess file to make it work in a way that when a desktop user attempts to go to m.website.com it remains in the desktop version.
Place this additional rule in .htaccess of DOCUMENT_ROOT of m.website.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^m\. [NC]
RewriteCond %{HTTP_USER_AGENT} !(android|blackberry|googlebot-mobile|iemobile|iphone|ipod|opera\smobile|palmos|webos) [NC]
RewriteRule ^ http://website.com%{REQUEST_URI} [L,R=302]

Redirecting mydomain.previewdns.com to mydomain.com (temporary mode)

Godaddy host provides the preview dns service ,i indexed mydomain.previewdns.com
and it displays a lot of results but it has a lot of problems as you know like not helping ranking for the original domain and also it is not showing my ads..
I tried:
RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST}
!^http://www.mydomain.previewdns\.com$ [NC] RewriteRule ^(.*)$
http://www.mydomain.com [R=302,L]
But this redirect rules redirects correctly in localhost(even am not asking for localhost redirecting) but when i put it remotely it makes the website offline.
There is no http in %{HOST_NAME} variable. Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} \.previewdns\.com$ [NC]
RewriteRule ^ http://www.mydomain.com%{REQUEST_URI} [R=302,L]

.htaccess causing endless loops, unable to rewrite .net to .com with a blog

I've got a domain with the .com and .net and they both point to the same location. I need both the main site and the blog to map to one url because Google is slamming me for duplicate content. It's a PHP site and a Wordpress Blog.
The ultimate goal is to rewrite each of these two sets to two distinct URL's. Is this possible? If it isn't possible can someone please recommend my closest option? Or maybe I should just eliminate the .net.
The .com is the primary site, and .net is parked to the same physical location.
otown411.com
www.otown411.com
otown411.net
www.otown411.net
--------------------
otown411.com <--- **goal rewrite of any 4 above**
blog.otown411.com
blog.otown411.net
www.otown411.com/blog
www.otown411.net/blog
otown411.com/blog
otown411.net/blog
---------------------
blog.otown411.com <--- goal rewrite from any six above
This is the closest I can get, it handles the top section fine, any attempts to get the blog section working result in endless loops:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{http_host} !^otown411\.com$ [NC]
RewriteRule ^(.*)$ http://otown411.com/$1 [R=301,NC,QSA,L]
This should do the trick:
Options +FollowSymLinks
RewriteEngine on
# Handle Blog first
RewriteCond %{HTTP_HOST} !^blog\.otown411\.com [NC] # check we're not where we want to be
RewriteCond %{HTTP_HOST} ^blog\. [NC,OR] # only if subdomo is blog OR...
RewriteCond %{REQUEST_URI} .*blog/.* [NC] # blog/ is in the URL
RewriteRule ^(blog/)?(.*?)$ http://blog.otown411.com/$2 [R=301,QSA,L]
# Now handle the rest
RewriteCond %{HTTP_HOST} !^blog\.otown411\.com [NC] # don't process for blog nor
RewriteCond %{HTTP_HOST} !^otown411\.com$ [NC] # if we're already on otown411.com
RewriteRule ^(.*)$ http://otown411.com/$1 [R=301,NC,QSA,L]

Categories