开发者

htaccess rewrite rule doesnt work in mobile browser

开发者 https://www.devze.com 2023-03-16 18:37 出处:网络
ive been trying to make a mobile version of my site and redirecting users with the htaccess file开发者_Python百科.

ive been trying to make a mobile version of my site and redirecting users with the htaccess file开发者_Python百科.

My site uses rewrite rules before to clean up urls, which works great. When I try to do the exact same rule but redirect to mobile.php instead of index.php, i get a 500 internal server error.

Here is the code:

RewriteEngine On

RewriteCond %{HTTP_USER_AGENT} #detect mobile browsers here...
RewriteRule ^([^/]*)[/]*([^/]*)$ /mobile.php?r=$1&id=$2 [L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]*)[/]*([^/]*)$ /index.php?r=$1&id=$2 [L]

Can anyone see my problem?


Yes -- this line as you post it is invalid for a few reasons:

RewriteCond %{HTTP_USER_AGENT} #detect mobile browsers here...
  1. Comment #detect mobile browsers here... can only be the first non-empty character in a line -- comment cannot start in the middle of the line.

  2. Because of #1, the line is incomplete -- it should have "Condition Pattern" part, which is missing (you have comment instead). See the docs for a full/correct syntax

  3. You may enter into a rewrite loop in this rule (a lot of people forgetting that after mod_rewrite does rewrite, it goes to another iteration, and if you not insert proper extra condition (RewriteCond) it may loop forever -- actually Apache will break it after it reaches the limit, but the point remains).


Although I'm not sure what the problem with the first code was, I have figured out something that does what I was trying to achieve.

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/index.php$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^([^/]*)[/]*([^/]*)$ /mobile.php?r=$1&id=$2 [L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^([^/]*)[/]*([^/]*)$ /mobile.php?r=$1&id=$2 [L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]*)[/]*([^/]*)$ /index.php?r=$1&id=$2 [L]

Is this good practice? Im pretty new to this (obviously).

0

精彩评论

暂无评论...
验证码 换一张
取 消