Forum Moderators: phranque

Message Too Old, No Replies

Rewrite Rule not working for Iphone or Ipad

Rewrite Rules mobile device

         

at36

12:41 pm on Nov 20, 2014 (gmt 0)

10+ Year Member



Hi all, I am new to this and hope you can help

I have a site e.g. www.example.net and want the site to go to the following URL www.example.com, but displaying www.example.net

I found this code that appeared to work, but in fact redirected to www.example.com displaying www.example.com as well (hence did not achieve what i wanted)

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.net
RewriteRule ^(.*) http://www.example.com/$1

On top of that I found out that the above rule does not work on Iphone or IPAD
Hence I added the following and came up with the following HACK (not pretty)
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteRule .* http://www.example.com
RewriteCond %{HTTP_HOST} ^www.example.net
RewriteRule ^(.*) http://www.example.com/$1

I do not have an IPAD but apparently it worked for the IPAD (BUT not the IPHONE) but again displayined www.example.com in the URL.

In short rewrite www.example.net to www.example.com on all types of devices
Thanks v much
A

[edited by: Ocean10000 at 4:09 pm (utc) on Nov 20, 2014]
[edit reason] examplified [/edit]

lucy24

7:03 pm on Nov 20, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{HTTP_HOST} ^www.example.net
RewriteRule ^(.*) http://www.example.com/$1

This rule creates an external redirect because the target includes a domain name. Did you find the rule quoted on some other site? Delete that site from your Reliable Sources list; any external redirect needs to end in the flags [R=301,L]. Without them, it becomes a temporary 302 redirect and, more lethally, rewriting doesn't stop. All RewriteRules need an [L] flag unless (a) it's implicit in some other flag like [F] or (b) you know exactly what you are doing. (In the present forum, that means: if your name is J.D. Morgan).

There's no earthly reason why the quoted rule would behave differently depending on user-agent. There must be something earlier intercepting iOS requests.

RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteRule .* http://www.example.com

Even worse, because now you've created an infinite loop. A RewriteCond applies only to the next following rule.


Now backtracking to your original question:
I have a site e.g. www.example.net and want the site to go to the following URL www.example.com, but displaying www.example.net

Is this happening in htaccess or config? Please say htaccess, because you're not ready to run your own server. If so, the answer is simple: It can't be done. Serving content from one host (domain) while the address bar shows a different host can only be done if the two hosts live on the same server. It then requires a Proxy [P] flag, which is only possible if you have use of mod_proxy. In general this can't be done in htaccess.

at36

7:27 pm on Nov 20, 2014 (gmt 0)

10+ Year Member



Hi Lucy 24, thanks for the prompt response.

Letme give you a quick background, basically I was trying to create a website for someone who has had theirs hacked, hence I set up in the latest version of joomla.
Only to find that their host does not run the latest version of php. I spoke to their host who said that they will not be updating to latest version for another month or so.

In the interim I deleted their hacked site as there were no reliable back ups and I guessed full of viruses.
Hence I wanted a temporary solution so that they could redirect / to a new URL but display their old, hence
if I type www.example.net it would show data from www.example.com, but still display www.example.net in the URL.

In short this was going to be done using .htaccess, i tried it simply with
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.net
RewriteRule ^(.*) http://www.example.com/$1

But then was told that it was redirecting and showing www.example.com in the URL, but also not displaying on iphone or IPAD. Hence the further hack which you have seen.

Correct it is not my intention to run my own server, I just want to get this working as an interim fix any help appreciated. Oh and correct two hosts are not on same server.

As for the infinite loop yep got it, but i was just interested in getting it to work, (BAD i know)

Any pointer for the .htaccess would be great

Thanks
A

lucy24

9:22 pm on Nov 20, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Unless you've got confirmation that you have use of mod_proxy, what you're trying to do simply cannot be done. Someone else may know more about this than I do. What I know is that (a) my own site (shared hosting) does not have use of mod_proxy and (b) the word "htaccess" does not occur anywhere in Apache's documentation page for mod_proxy (2.2 and 2.4). Rewriting to a different hostname is simply a whole nother issue than rewriting to a different URL within the same host.

If you set your flags as [R,L] without the "301" part-- or even if you just say [L] with no mention of [R] at all-- it will be returned as a temporary redirect. This may be a viable temporary fix, since it's supposed to mean that search engines will continue to index the old URL. (I stress: "supposed to". Better check in one of the google subforums to make sure this is still the case.)

Alternately you could change the DNS so old-domain-name points to the same physical location as new-domain-name. If the two live on the same server (this doesn't seem likely, or they'd both have been hacked) and you've got an accommodating host it may be possible for this to be done server-side without a DNS change.

Having two different domains displaying identical content is not a good long-term fix, no matter how you do it.

at36

9:49 pm on Nov 20, 2014 (gmt 0)

10+ Year Member



:( oh well looks like in my keeness (if that word exists) to help, I have given myself more work.

Yep it was meant to be a short term fix, now i will have to work quickly to either make another site on a previous version or carry out a host and DNS change, which will mean a little down time.

mmmmm oh well

Thanks v much for the help

:)