I have wordpress site and need advise regarding redirect htaccess based on useragent:
Sample:
my old url: myolddomain/old-post/ redirect to: mynewdomain/new-post/
based on useragent
thanks
I have wordpress site and need advise regarding redirect htaccess based on useragent:
Sample:
my old url: myolddomain/old-post/ redirect to: mynewdomain/new-post/
based on useragent
thanks
Share Improve this question asked May 31, 2019 at 12:56 itcomindoitcomindo 12 Answers
Reset to default 0using 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'
}
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]
RewriteCond %{HTTP_USER_AGENT} MSIE
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/iphone", 301 );
} else if( stristr($_SERVER['HTTP_USER_AGENT'],'android') ) {
wp_redirect( "http://www.example/andriod", 301 );
}
}
}