简体   繁体   English

.htaccess中带有漂亮URL的移动重定向

[英]Mobile Redirect with pretty URLs in .htaccess

I am trying to redirect my main site (www.mainsite.com) to my mobile site (m.mainsite.com) based on user agent in .htacess. 我正在尝试根据.htacess中的用户代理将主站点(www.mainsite.com)重定向到移动站点(m.mainsite.com)。 I used the following code and it redirects to the m.mainsite.com properly. 我使用以下代码,并将其正确重定向到m.mainsite.com。

 RewriteCond %{HTTP_USER_AGENT} (Android|AU-MIC|AUDIOVOX|ALCATEL|Blackberry|Blazer|Googlebot-mobile|Handheld|iPhone|iPod|Klondike|LG-|LGE-|Nokia|NokiaN8|Opera\\ Mini|PalmOS|PalmSource|Smartphone|Symbian|WebOS|Windows\\ CE|Windows\\ Mobile|Windows\\ Phone|nokia|UP.Link|UP.Browser) RewriteRule (.*) http://m.mainsite.com/$1 

However, when taken to the mobile site I seem to be losing the rewritten URL. 但是,当带到移动网站时,我似乎丢失了重写的URL。 So what should be m.mainsite.com/page1 goes to something like m.mainsite.com/index.php?url=page1. 因此,应该将m.mainsite.com/page1更改为m.mainsite.com/index.php?url=page1之类的内容。

I tried rewriting the URL using .htaccess on the mobile site but could not figure out how to remove the part after index.php. 我尝试在移动网站上使用.htaccess重写URL,但无法弄清楚如何在index.php之后删除该部分。 Can anyone help me figure out how to remove "index.php?url=" from m.mainsite.com/index.php?url=page1? 谁能帮助我找出如何从m.mainsite.com/index.php?url=page1中删除“ index.php?url =“?

Or would it be easier to figure out how to address this via htaccess in the non-mobile version during the redirect. 还是更容易弄清楚如何在重定向期间通过非移动版本的htaccess解决此问题。

Full code looks like: 完整代码如下:

 [code] RewriteCond %{HTTP_HOST} ^mainsite.com RewriteRule (.*) http://www.mainsite.com/$1 [R=301,L] RewriteRule ^(.*)\\_(\\?.*)?$ $1$2 [R=301,L] RewriteRule .* - [env=REWRITE_ON:1] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !\\.(js|css|png|jpg|gif)$ RewriteRule ^(.*)$ index.php?url=$1 [L] RewriteCond %{HTTP_USER_AGENT} (Android|AU-MIC|AUDIOVOX|ALCATEL|Blackberry|Blazer|Googlebot-mobile|Handheld|iPhone|iPod|Klondike|LG-|LGE-|Nokia|NokiaN8|Opera\\ Mini|PalmOS|PalmSource|Smartphone|Symbian|WebOS|Windows\\ CE|Windows\\ Mobile|Windows\\ Phone|nokia|UP.Link|UP.Browser) RewriteRule (.*) http://m.mainsite.com/$1[code] 

Thanks Gary 谢谢加里

Looks like you have other rules that's interferring. 看来您还有其他干扰规则。 You need to put all of your redirect rules before any internal routing rules. 您需要将所有重定向规则放在任何内部路由规则之前 The index.php?url=page1 looks like your routing rule. index.php?url=page1看起来像您的路由规则。

Place the mobile redirect near the top of your htaccess file. 将移动设备重定向放置在htaccess文件顶部附近。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM