Q:

How do I redirect specific URL's in the .htaccess file

I want to redirect specific pages only to the mobile version of my website. I can redirect the homepage with the code below but I can't find a way to redirect specific pages. I want to redirect https://www.richmond-medical.com/sm3 to http://m.richmond-medical.com/sm3 .
My problem is when I type in Richmond-medical.com/sm3 on a mobile it goes to http://richmond-medical.com only.
Here is my code:
<IfModule mod_rewrite.c>

RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]

RewriteCond %{HTTP_HOST} ^www.Richmond-Medical.com$ [NC]    
RewriteCond %{REQUEST_URI} ^/sm3$    
RewriteRule ^/?a/(.*)$ http://m.richmond-medical.com/$ [R=301,L]

RewriteCond %{HTTP_HOST} richmond-medical.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://m.richmond-medical.com [L,R=301]

</IfModule>

A:

The working code for the above requirement is below
RewriteEngine on

RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(R|r)ichmond-medical.com$ [NC]    
RewriteCond %{REQUEST_URI} ^/sm3$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://m.richmond-medical.com/$1 [R=301,L]

I have clubbed both the rule in an [OR] clause so that root domain redirects to mobile one and sm3 redirects to mobile one.
The .htaccess rule can be tested at .htaccess tester
In case you have some new pages that needs to be redirected to mobile one you can add it to the [OR] clause like if we need sm5 to be redirected we just need to add sm5 in [OR] clause. We can also club it in the same rewrite condition using | like below.
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(R|r)ichmond-medical.com$ [NC]    
RewriteCond %{REQUEST_URI} ^(/sm3|/|/sm5)$
RewriteRule ^(.*)$ https://m.richmond-medical.com/$1 [R=301,L]

