子域名正在接收域名请求

时间:2012-04-17 21:09:14

标签: .htaccess mod-rewrite subdomain

我已经通过 cPanel 为我的网站创建了一个名为mobile的子域名。我将移动设备重定向到该子域,但是有一些javascript存在于那里,使AJAX调用到实际域。我已将这些调用结构化为website.com/mobile/...。但是,这些都没有通过,我怀疑这是因为它在...中寻找/mobile,但请求应该在 .htaccess 中重写到website.com/index.php?params=mobile/...

这是.htaccess:

# redirect phones/tablets to mobile site
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} !mobile\.website\.com [NC]
RewriteCond %{REQUEST_URI} !^/mobile [NC]
RewriteRule ^(.*)$ http://www.mobile.website.com/$1 [L,R=302]

# not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# website.com/home => website.com/index.php?params=home
RewriteRule ^(.+)(\?.+)?$ index.php?params=$1 [L,QSA]

这适用于我的本地计算机,但不适用于实时服务器。我已经通过

在本地创建了一个sudomain
<VirtualHost *:80>
    DocumentRoot "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs/website/mobile"
    ServerName mobile.website.local
</VirtualHost>

并且效果很好:当我转到mobile.website.localwebsite.local/mobile时,我会访问移动网站,当我转到website.local/mobile/users/login时,我会获得AJAX请求的正确JSON输出

如何在mobile中保持我的/mobile/子域名存活,但是有website.com/mobile/...请求与最后一次重写规则一起转发?

谢谢!

1 个答案:

答案 0 :(得分:0)

只需添加/ mobile的特定重定向,强制忽略文件或目录语句:

RewriteCond %{REQUEST_URI} ^/mobile [NC]
RewriteRule ^(.+)(\?.+)?$ index.php?params=$1 [L,QSA]

# redirect phones/tablets to mobile site
RewriteCond %{HTTP_HOST} !mobile\.website\.com [NC]
RewriteCond %{REQUEST_URI} !^/mobile [NC]
RewriteCond %{QUERY_STRING}  !^params=mobile(.*)$
RewriteRule ^(.*)$ http://www.mobile.website.com/$1 [L]

# not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# website.com/home => website.com/index.php?params=home
RewriteRule ^(.+)(\?.+)?$ index.php?params=$1 [L,QSA]

任何事都让我知道,我会看看我能不能帮忙:)。