0

i'm currently using this to redirect domain.com/username to the following address

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ publicprofile.php?username=$1&%{QUERY_STRING}

I am developing a mobile site, therefore i need to redirect mobile users to mobile site.

this are the code i found for mobile redirecting

RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$
RewriteRule ^(.*)$ http://mobile.yourdomain.com [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*BlackBerry.*$
RewriteRule ^(.*)$ http://mobile.yourdomain.com [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*Palm.*$
RewriteRule ^(.*)$ http://mobile.yourdomain.com [R=301]

how do i make both of them work at the same time?

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
Damian Sia
  • 479
  • 1
  • 4
  • 14

2 Answers2

1

Allow me optimize your rules:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (iphone|blackberry|palm|android) [NC]
RewriteRule ^(.*)$ http://mobile.yourdomain.com [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ publicprofile.php?username=$1 [QSA,L]

[NC] stand for 'case insensitive'. [QSA] is to append the query string. You can test your rules at mod_rewrite tester.

DrDol
  • 2,220
  • 2
  • 19
  • 23
  • I'm a bit hypercritical with grammar probably, but `[NC]` stands for `No Case` not `case insensitive`. They are synonyms, but when expanding an _acronym_ you usually want to be exact for the benefit of exhibiting what each character of the _acronym_ literally represents. – chown Sep 09 '11 at 05:58
  • [`[NE]`](http://stackoverflow.com/a/6529394/712308) [also](http://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_ne) maybe usefull. – Artem P May 26 '14 at 15:01
0

Put the second block before the first, and you should be good to go.

The second block will do header redirects if needed, which will cause a new request to happen. After that, the internal rewriting rules have their turn.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088