简单的子域重定向PHP

时间:2013-10-13 18:20:22

标签: php .htaccess subdomain

我的移动应用有一个子域,就像http://m.traffic.domain.com 现在我希望我的用户可以访问http://m.traffic.domain.com/username来访问他们的网页 它将在内部指向http://m.traffic.domain.com/index.php?username= $ 1 为此,我有以下.htaccess重写代码,但它没有按预期工作,而是将所有页面重定向到http://m.traffic.domain.com/index.php?username= $ 1 页

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
AddHandler application/x-httpd-php .aspx
RewriteEngine On
RewriteCond %{HTTP_HOST} ^m\.traffic\.domain\.com$  [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^(.+)$ mobile/index.php?username=%1 [L]
<Files 403.shtml>
order allow,deny
allow from all
</Files>

子域名http://m.traffic.domain.com指向名为mobile的目录,可以访问该目录http://traffic.domain.com/mobile

1 个答案:

答案 0 :(得分:2)

请尝试使用此代码:

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
AddHandler application/x-httpd-php .aspx

RewriteEngine On

RewriteCond %{HTTP_HOST} ^m\.traffic\.domain\.com$  [NC]
RewriteRule ^([^.]+)/?$ /index.php?username=$1 [L,QSA]

<Files 403.shtml>
order allow,deny
allow from all
</Files>

来自RewriteRule的匹配群组为$1, $2而不是%1, %2,您不需要/mobile,因为您的请求是m.traffic.domain.com而不是traffic.domain.com }

相关问题