Reputation: 5298
I currently have a website with a standard web interface on index.php
, and I made an iPhone-friendly version in iphone.php
.
Both pages handle the same arguments.
It works fine when I manually go to .../iphone.php
, but I'd like to rewrite anything on .../path/
and .../path/index.php
to iphone.php
if %{HTTP_USER_AGENT}
contains mobile
, and optionally add the query string (not sure if/when I'd need to add it).
So far, this is what I have in my .../path/.htaccess
:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^.+mobile.+$ [NC]
RewriteRule index.php?(.*) iphone.php?$1 [L]
RewriteRule index.php iphone.php [L]
The problems are, it matches index.php
in any subfolder, and it won't match .../path/?args
…
Upvotes: 0
Views: 1223
Reputation: 4915
RewriteCond
is applied to exactly one rewrite rule..*
- 0 or more any character should be used in ^.+mobile.+$
statement.RewriteRule
by default doesn't include query string - [QSA]
flag should be used to include query string.RewriteRule
uses regexp - you should use \.
to escape dots.RewriteRule
automatically saves query string the same, if not specified another.UPDATED:
Complete .htacess:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^.*mobile.*$ [NC]
RewriteRule ^(.*)index\.php $1iphone.php [L]
RewriteCond %{HTTP_USER_AGENT} ^.*mobile.*$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1iphone.php [L]
Upvotes: 1
Reputation: 1237
How about
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^.*mobile.*$ [NC]
RewriteRule /path/index.php /path/iphone.php [QSA,L]
EDIT: QSA is Query string append
, so you don't need to handle that differently. If you want you can add an extra parameter too, like:
RewriteRule /path/index.php /path/iphone.php?extra=param [QSA,L]
Upvotes: 0