How to redirect specific user agent on specific url to speci

Problems with the Linux version of XAMPP, questions, comments, and anything related.

How to redirect specific user agent on specific url to speci

Postby haryono » 20. May 2015 20:32

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
haryono
 
Posts: 4
Joined: 20. May 2015 20:28
Operating System: Windows 7

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

Postby glitzi85 » 20. May 2015 22:34

"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?
User avatar
glitzi85
 
Posts: 1920
Joined: 05. March 2004 23:26
Location: Dahoim

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

Postby haryono » 20. May 2015 22:42

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
haryono
 
Posts: 4
Joined: 20. May 2015 20:28
Operating System: Windows 7

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

Postby glitzi85 » 20. May 2015 23:09

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.
User avatar
glitzi85
 
Posts: 1920
Joined: 05. March 2004 23:26
Location: Dahoim

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

Postby haryono » 21. May 2015 07:02

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]
haryono
 
Posts: 4
Joined: 20. May 2015 20:28
Operating System: Windows 7

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

Postby Nobbie » 21. May 2015 11:13

>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.
Nobbie
 
Posts: 8770
Joined: 09. March 2008 13:04

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

Postby glitzi85 » 21. May 2015 22:34

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)
User avatar
glitzi85
 
Posts: 1920
Joined: 05. March 2004 23:26
Location: Dahoim

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

Postby haryono » 22. May 2015 19:02

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
haryono
 
Posts: 4
Joined: 20. May 2015 20:28
Operating System: Windows 7

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

Postby glitzi85 » 22. May 2015 19:30

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
User avatar
glitzi85
 
Posts: 1920
Joined: 05. March 2004 23:26
Location: Dahoim

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

Postby Nobbie » 22. May 2015 20:25

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.
Nobbie
 
Posts: 8770
Joined: 09. March 2008 13:04


Return to XAMPP for Linux

Who is online

Users browsing this forum: No registered users and 9 guests