Page 1 of 1

How to redirect specific user agent on specific url to speci

PostPosted: 20. May 2015 20:32
by haryono
I have question:

How to create redirect specific user agent on specific url to specific url in htaccess?

e.g

I want to redirect 301 with conditional:

Code: Select all
useragent: Firefox
in my url1: http://sampledomain.tld/old-urls1/
in my url2: http://sampledomain.tld/old-urls12/


to

Code: Select all
new url1: in my url1: http://sampledomain.tld/new-urls1/
new url2: in my url1: http://sampledomain.tld/old-urls2/


I try this in .htaccess

Code: Select all
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} googlebot [NC]
RewriteRule ^/?this-is-url/?$ http://newurl.tld/this-url [L,R,NC]


but not work

Thanks so much

Re: How to redirect specific user agent on specific url to s

PostPosted: 20. May 2015 22:34
by glitzi85
"Does not work" is not a qualified error report!

If I use your .htaccess and set my UA to googlebot it will redirect me, so the rules are correct. What is not working and how exactly are you testing your rules?

Re: How to redirect specific user agent on specific url to s

PostPosted: 20. May 2015 22:42
by haryono
I try it using different useragent Firefox

Code like this:
Code: Select all
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Firefox
RewriteRule ^/?this-is-url/?$ http://newurl.tld/this-url [L,R=301]


I try to using Chrome but still not work

Re: How to redirect specific user agent on specific url to s

PostPosted: 20. May 2015 23:09
by glitzi85
Do you actually know how Useragent-String usually look like?

Firefox@W7
Code: Select all
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0

Chrome@W7
Code: Select all
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36


In both browsers you can install extensions (search for "user agent switcher") to set your desired string (e.g. googlebot). You might have to try some different extensions as some only let you choose between predefined settings while others let you define own strings.

Re: How to redirect specific user agent on specific url to s

PostPosted: 21. May 2015 07:02
by haryono
Do you actually know how Useragent-String usually look like? with easily I can check it here

Code: Select all
http://www.whatsmyua.com/ or here http://whatsmyuseragent.com/


but it doesn't matter if I put Firefox, Chrome, Googlebot etc like this:

Code: Select all
RewriteCond %{HTTP_USER_AGENT} Googlebot [OR]
RewriteCond %{HTTP_USER_AGENT} msnbot [OR]
RewriteCond %{HTTP_USER_AGENT} Slurp


In both browsers you can install extensions (search for "user agent switcher") to set your desired string (e.g. googlebot). You might have to try some different extensions as some only let you choose between predefined settings while others let you define own strings.

I tried but not working!

Question is: can you answer my question or not? just leave it if you don't! please straight to the answer and here is my question!

How to create redirect specific user agent on specific url to specific url in htaccess?

e.g

I want to redirect 301 with conditional:


Code: Select all
    useragent: Firefox
    in my url1: http://sampledomain.tld/old-urls1/
    in my url2: http://sampledomain.tld/old-urls12/




to

Code: Select all
    new url1: in my url1: http://sampledomain.tld/new-urls1/
    new url2: in my url1: http://sampledomain.tld/old-urls2/




I try this in .htaccess

Code: Select all
    RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} googlebot [NC]
    RewriteRule ^/?this-is-url/?$ http://newurl.tld/this-url [L,R,NC] 


I need simple answer: how to change it with the correct one? RewriteRule ^/?this-is-url/?$ http://newurl.tld/this-url [L,R,NC]

Re: How to redirect specific user agent on specific url to s

PostPosted: 21. May 2015 11:13
by Nobbie
>I need simple answer: how to change it with the correct one? RewriteRule ^/?this-is-url/?$ http://newurl.tld/this-url [L,R,NC]

Simple: syntactically and theoretically it looks right.

But probably there seems to be an error, mostly in the pattern or similar. We cannot debug this for you, you should go step by step and apply simple example patterns etc. - it is quite a pain to debug .htaccess

I would recommend

a) first of all apply a very simple RewriteRule, which matches always, in order to find out, if the Condition is true or not.

b) at next, run a a test on your RewriteRule, simply remove the condition above, so that this line is executed in any way. This will give you a clue, if there is a problem with the RewriteRule


Step by step you will find out, why it does not work as expected. There is no debugger for that task.

Re: How to redirect specific user agent on specific url to s

PostPosted: 21. May 2015 22:34
by glitzi85
haryono wrote:I need simple answer: how to change it with the correct one? RewriteRule ^/?this-is-url/?$ http://newurl.tld/this-url [L,R,NC]

Simple Answer: This is a correct one. It might not work as you expect, but as long as you do not specify what you expect we will not be able to help you.

If I use your .htaccess and put it into my own htdocs I will get redirected to http://newurl.tld/this-url when I try to access http://localhost/this-is-url with Useragent set to googlebot (Of course the redirect will fail as newurl.tld is not a valid URI, but the redirect happens)

Re: How to redirect specific user agent on specific url to s

PostPosted: 22. May 2015 19:02
by haryono
use your firefox then visit

http://gordenminimalis.web.id/

I put my code

Please your firefox first

then visit:

Code: Select all
gordenminimalis.web.id


Code: Select all
RewriteEngine On
RewriteCond %{HTTP_HOST} gordenminimalis.web.id$ [NC]
RewriteCond %{HTTP_USER_AGENT} Firefox [OR]
RewriteCond %{HTTP_USER_AGENT} msnbot [OR]
RewriteCond %{HTTP_USER_AGENT} Slurp
RewriteRule ^(.*)$ http://bing.com/$1 [L,R=301]


After that change your useragent or your browser + visit again

I try it with chrome and it's work (no redirect to bing)

the problem is when I want to redirect urls from gordenminimalis.web.id e.g http://www.gordenminimalis.web.id/product/contoh-produk-4/ so far I can't found best solution

Re: How to redirect specific user agent on specific url to s

PostPosted: 22. May 2015 19:30
by glitzi85
Now we are getting closer. Obviously your RewriteConditions work.
haryono wrote:the problem is when I want to redirect urls from gordenminimalis.web.id e.g http://www.gordenminimalis.web.id/produ ... -produk-4/ so far I can't found best solution

Where is the problem? You already had the expression right:
Code: Select all
RewriteRule ^product/contoh-produk-4/?$ http://bing.com/$1 [L,R=301]

This will redirect http://gordenminimalis.web.id/product/contoh-produk-4 and http://gordenminimalis.web.id/product/contoh-produk-4/ to bing

Re: How to redirect specific user agent on specific url to s

PostPosted: 22. May 2015 20:25
by Nobbie
On my PC everything works correctly.

I added gordenminimalis.web.id to my hosts file /etc/hosts:

Code: Select all
127.0.0.1  gordenminimalis.web.id


I added a folder "rewrite" in my htdocs folder and put a .htaccess with this contents:

Code: Select all
RewriteEngine On
RewriteCond %{HTTP_HOST} gordenminimalis.web.id$ [NC]
RewriteCond %{HTTP_USER_AGENT} Chrome [OR]
RewriteCond %{HTTP_USER_AGENT} Firefox [OR]
RewriteCond %{HTTP_USER_AGENT} msnbot [OR]
RewriteCond %{HTTP_USER_AGENT} Slurp
RewriteRule ^(.*)$ http://bing.com/$1 [L,R=301]


And when i call http:// gordenminimalis.web.id/rewrite in my Browser (as well Firefox as Chrome), i am redirected to Bing.