0

My site is: example.com

My mobile version: m.example.com

Profile page is example.com/profile or m.example.com/profile

Or posting page example.com/posts or m.example.com/posts

Mobile version's url is similar. So, how to redirect the same page? For example: user go (from facebook) into example.com/posts but he uses mobile device, so how to redirect via .htaccess to m.example.com/posts

I saw this one threat, but i'm confuse to create right rules.

Thanks :)

Community
  • 1
  • 1

1 Answers1

0

Your question is similar to this question, except that you want to redirect the user. First you need a list of mobile user-agents. I am not going to suggest a list, but I think you should be able to find a list that is suitable for you.

Next, you want to go to your .htaccess file and add something like this:

RewriteCond %{HTTP_USER_AGENT} ^(user-agent1|user-agent2|user-agent3|etc)$
RewriteCond %{HTTP_HOST} !^m\.example\.com$
RewriteRule ^(.*)$ http://m.example.com/$1 [R,L]

The first RewriteCond contains all user-agents you want to redirect. Remember that it is a regex, so you have to escape any special characters! The second RewriteCond checks if the host isn't already m.example.com. If we wouldn't check for this, we would create an infinite loop, redirecting to the same url over and over. The RewriteRule itself matches everything after the hostname (and before the query string) and redirects it to the mobile site. $1 gets replaced with the first capture group in the regex in the first argument of RewriteRule. The [R] flag is not necessary in this case (links with http:// will always be redirected), but it let's Apache know that it should do an external temporary redirect to the new url. The [L] flag will stop processing more rules if this rule matches.

See the documentation for more information.

Community
  • 1
  • 1
Sumurai8
  • 20,333
  • 11
  • 66
  • 100