.htaccess扩展子域URL重写

时间:2011-12-11 09:21:35

标签: apache .htaccess url mod-rewrite rewrite

我想保留domain.com/whatever& amp; www.domain.com/whatever我的主要应用网站,作为以下内容的前身。

对于我的用户,我的主页设置为username.domain.com。这很好,并显示他们的页面,但我遇到的问题是当我尝试处理username.domain.com/one,username.domain.com/one/two,username.domain.com/one/two/three < / p>

目前我所拥有的成功发送子域名为u.php

    RewriteCond %{HTTP_HOST} ^(^.*)\.domain\.com$
    RewriteRule ^(.*)$  u.php

我不需要改变它(它的工作原理)。我现在需要的是:

  1. username.domain.com写入u.php(目前使用上面的表达式工作得非常好)
  2. username.domain.com/one写入a.php?request = one
  3. username.domain.com/one/two写入s.php?request = one&amp; ident = two
  4. 修改

    以下是我调整应用程序以运行用户子域之前的一些表达式,只是为了帮助:

        RewriteRule ^([a-z\-]+)/?$ a.php?request=$1 [L]
        RewriteRule ^/([a-z]+)/([a-z0-9\-]+)/?$ /s.php?request=$1&ident=$2 [L]
        RewriteRule ^u/([a-z]+)/([a-z]+)/([a-z0-9\-]+)/?$ /s.php?user=$1&request=$2&ident=$3 [L]
    

    请记住

    我的问题是,目前所有内容都是user.domain.com/one未被重写。它仍然是u.php,当我想要它去s.php?request = $ 1

    再次感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你的第一行实际上在说“不管它是什么,把它发送到u.php”:

RewriteRule ^(.*)$  u.php

您在这里遇到的一个问题是您放置表达式的顺序。因为你的[L]旗帜...... http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_l

RewriteRule ^([a-z\-]+)/?$ a.php?request=$1 [L] ## <- would match one/two/three and would stop the script from continuing because of the [L] flag.
RewriteRule ^/([a-z]+)/([a-z0-9\-]+)/?$ /s.php?request=$1&ident=$2 [L]
RewriteRule ^u/([a-z]+)/([a-z]+)/([a-z0-9\-]+)/?$ /s.php?user=$1&request=$2&ident=$3 [L]

可能的解决方案

我已将所有意图连接到一个匹配表达式中,我希望它适合您...

RewriteRule ^([a-z\-]+)?/?([a-z\-]+)?/?([a-z\-]+)?/?$  u.php?first=$1&second=$2&third=$3 [L]
RewriteRule ^(.*) u.php [L]

此代码会向您发送一些空参数...您应该在PHP代码中查看这些参数。

如果你真的不想要,你可以随时拆分代码......

RewriteRule ^([a-z\-]+)/([a-z\-]+)/([a-z\-]+)/?$  u.php?first=$1&second=$2&third=$3 [L]
RewriteRule ^([a-z\-]+)/([a-z\-]+)/?$  u.php?first=$1&second=$2 [L]
RewriteRule ^([a-z\-]+)/?$  u.php?first=$1 [L]
RewriteRule ^(.*) u.php [L]

注意表达式的顺序。第三个表达式实际上匹配一个/两个/三个。

我刚才在这里使用[a-z-]。查看您自己的代码,我想您知道如何添加对数字或大写字符的支持。

相关问题