I have wordpress site and need advise regarding redirect htaccess based on useragent:


my old url: myolddomain/old-post/ redirect to: mynewdomain/new-post/

based on useragent



using Javascript to get the User Agent. Then based on the User Agent do a redirect.

something like this:

var ua = navigator.userAgent; 

if (ua === 'whatever userAgent you are testing for')
   window.location = 'https://newurl.com'

You can write simple .htaccess redirects based on user-agents something like this.

RewriteEngine on

RewriteCond %{HTTP_USER_AGENT} Opera
RewriteRule ^abc.html$ opera.html [NC,L]

RewriteRule ^abc.html$ msie.html [NC,L]

RewriteCond %{HTTP_USER_AGENT} Chrome
RewriteRule ^abc.html$ chrome.html [NC,L]

RewriteCond %{HTTP_USER_AGENT} Safari
RewriteRule ^abc.html$ safari.html [NC,L]

RewriteCond %{HTTP_USER_AGENT} Firefox
RewriteRule ^abc.html$ firefox.html [NC,L]

RewriteRule ^abc.html$ default.html [L]

In WordPress, there is also some inbuilt action hooks such as "template_redirect" You can use this action hook too.

add_action( 'template_redirect', 'device_redirect' );

function device_redirect(){
  if ( is_front_page() && is_home() ) {
    if( stristr($_SERVER['HTTP_USER_AGENT'],'iphone') || strstr($_SERVER['HTTP_USER_AGENT'],'iphone') ) {
            wp_redirect( "http://www.example.com/iphone", 301 );
    } else if( stristr($_SERVER['HTTP_USER_AGENT'],'android') ) {
           wp_redirect( "http://www.example.com/andriod", 301 );
  • Note that User Agent values can be spoofed, so this is not foolproof. May 31 '19 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.