【问题标题】:dynamic subdomains using .htaccess in php在 php 中使用 .htaccess 的动态子域
【发布时间】:2014-04-01 18:55:43
【问题描述】:

我正在尝试实现动态

RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^/?$ /live/agent/index.php?agent_user_name=%1 [L]

RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^(.+?)/?$ /live/agent/$1.php?agent_user_name=%1 [L]

现在这个 .htaccess 转换了

http://mydomain.com/live/agent/index.php?agent_user_name=username 到 http://username.mydomain.com

和

http://mydomain.com/live/agent/forum.php?agent_user_name=username 到 http://username.mydomain.com/form/

但是,还有其他页面我想重定向到子域,例如

http://mydomain.com/live/agent/view_blog.php?agent_user_name=username&blog_id=19 这个页面应该通过子域来阅读,比如http://username.mydomain.com/view_blog/19 等等

http://mydomain.com/live/agent/page.php?agent_user_name=username&content_id=19此页面应由http://username.mydomain.com/content/19等访问

谢谢

【问题讨论】:

    标签: .htaccess subdomain


    【解决方案1】:

    这应该可行:

    RewriteEngine on
    
    RewriteCond %{QUERY_STRING} ^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
    RewriteRule ^content/([0-9]+)/?$ /live/agent/page.php?agent_user_name=%1&blog_id=$1 [L,QSA]
    
    RewriteCond %{QUERY_STRING} ^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
    RewriteRule ^([^/]+)/([0-9]+)/?$ /live/agent/$1.php?agent_user_name=%1&blog_id=$2 [L,QSA]
    
    RewriteCond %{QUERY_STRING} ^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
    RewriteRule ^/?$ /live/agent/index.php?agent_user_name=%1 [L]
    
    RewriteCond %{QUERY_STRING} ^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
    RewriteRule ^(.+?)/?$ /live/agent/$1.php?agent_user_name=%1 [L]
    

    【讨论】:

    • anubhava,你没有在 .htaccess 规则中提到 page_id 变量...... ???可以有更多额外的变量,比如博客部分,我们可能有 blog.php?agent_user_name=username&page=1&search=property ????
    • 您的示例中没有page_id。对于更多变量,您可以遵循我建议的相同模式并扩展额外变量的规则。
    • 重写规则 ^([^/]+)/([0-9]+)/?$ /live/agent/$1.php?agent_user_name=%1&blog_id=$2 [L,QSA]这个规则,你能告诉我如果我用page_id改变blog_id,我怎样才能让它访问page.php而不是view_blog.php???
    【解决方案2】:

    在您当前的规则下方添加以下内容应该可以达到您想要的效果。

    RewriteCond %{THE_REQUEST} ^(GET|POST)\ /live/agent/view_blog.php\?agent_user_name=(.*)&blog_id=(.*)\ HTTP
    RewriteRule ^ http://%2.mydomain.com/view_blog/%3? [R,L]
    
    RewriteCond %{THE_REQUEST} ^(GET|POST)\ /live/agent/page.php\?agent_user_name=(.*)&content_id=(.*)\ HTTP
    RewriteRule ^ http://%2.mydomain.com/content/%3? [R,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      相关资源
      最近更新 更多