开发者

htaccess double rewrite rule

开发者 https://www.devze.com 2023-02-10 08:37 出处:网络
i\'m currently using this to redirect domain.com/username to the following address RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d

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.yourdom开发者_StackOverflow中文版ain.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?


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.


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.

0

精彩评论

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