Simple subdomain redirect PHP

↘锁芯ラ 提交于 2019-12-24 05:15:06

问题


I have a sub-domain for my mobile app which is like http://m.traffic.domain.com now I want that my users can access their page by visiting http://m.traffic.domain.com/username which will internally point to http://m.traffic.domain.com/index.php?username=$1 for this I have following .htaccess rewrite code but its not working as expected rather its redirecting all pages to http://m.traffic.domain.com/index.php?username=$1 this page

# 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>

The subdomain http://m.traffic.domain.com points to a directory called mobile which can be accessed http://traffic.domain.com/mobile


回答1:


Try this code instead:

# 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>

Matched groups from RewriteRule are $1, $2 instead of %1, %2 and you don't need /mobile since your request is for m.traffic.domain.com not traffic.domain.com



来源:https://stackoverflow.com/questions/19348615/simple-subdomain-redirect-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!