尝试使用RewriteRule匹配子域和路径,只获取子域

时间:2013-03-18 18:52:34

标签: regex apache .htaccess mod-rewrite

我正在本地项目中进行一些测试(使用vHosts)。我设法创建了一个RewriteRule来将something.kensaku.com重定向到www.kensakuapp.com/something,但我想将something.kensaku.com/myapp重定向到www.kensakuapp.com/something/myapp

我的重写规则就是这个:

Options +FollowSymLinks

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(^.*)\.kensaku.com
RewriteRule ^(^.*) goto.php?username=%1&path=$1

我已经尝试了好几个小时,但仍然无法获得/myapp部分。如果我转到something.kensaku.com/myapp$1将与goto.php匹配,但不应与/myapp匹配?

以下是我用于重定向的goto.php脚本的内容:

<?php 
$user = $_GET['username'];
header("Location: http://www.kensakuapp.com/$user/".$_GET['path']); 
?>

如果还有其他方法可以不使用PHP,我会很感激知道它 - 但如果有可能我会继续使用PHP方法,因为将来或许我想要添加跟踪或记录功能。

如果我错过了任何问题,请告诉我!

编辑:记录rewritemod,跟踪3:

[Wed Mar 20 19:26:28.624291 2013] [rewrite:trace1] [pid 24190] mod_rewrite.c(467): [client 127.0.0.1:52996] 127.0.0.1 - - [www.kensaku.com/sid#982bb28][rid#9a7d120/initial] [perdir /opt/lampp/htdocs/kensaku/] pass through /opt/lampp/htdocs/kensaku/
[Wed Mar 20 19:26:28.624352 2013] [rewrite:trace1] [pid 24190] mod_rewrite.c(467): [client 127.0.0.1:52996] 127.0.0.1 - - [www.kensaku.com/sid#982bb28][rid#9a7b118/subreq] [perdir /opt/lampp/htdocs/kensaku/] pass through /opt/lampp/htdocs/kensaku/index.html
[Wed Mar 20 19:26:28.624402 2013] [rewrite:trace1] [pid 24190] mod_rewrite.c(467): [client 127.0.0.1:52996] 127.0.0.1 - - [www.kensaku.com/sid#982bb28][rid#9a7b118/subreq] [perdir /opt/lampp/htdocs/kensaku/] pass through /opt/lampp/htdocs/kensaku/index.html.var
[Wed Mar 20 19:26:28.624434 2013] [rewrite:trace1] [pid 24190] mod_rewrite.c(467): [client 127.0.0.1:52996] 127.0.0.1 - - [www.kensaku.com/sid#982bb28][rid#9a7b118/subreq] [perdir /opt/lampp/htdocs/kensaku/] pass through /opt/lampp/htdocs/kensaku/index.php
[Wed Mar 20 19:26:28.630172 2013] [rewrite:trace3] [pid 24190] mod_rewrite.c(467): [client 127.0.0.1:52996] 127.0.0.1 - - [www.kensaku.com/sid#982bb28][rid#9a7f2f0/initial] [perdir /opt/lampp/htdocs/kensaku/] strip per-dir prefix: /opt/lampp/htdocs/kensaku/ -> 
[Wed Mar 20 19:26:28.630202 2013] [rewrite:trace3] [pid 24190] mod_rewrite.c(467): [client 127.0.0.1:52996] 127.0.0.1 - - [www.kensaku.com/sid#982bb28][rid#9a7f2f0/initial] [perdir /opt/lampp/htdocs/kensaku/] applying pattern '^.+$' to uri ''
[Wed Mar 20 19:26:28.630216 2013] [rewrite:trace1] [pid 24190] mod_rewrite.c(467): [client 127.0.0.1:52996] 127.0.0.1 - - [www.kensaku.com/sid#982bb28][rid#9a7f2f0/initial] [perdir /opt/lampp/htdocs/kensaku/] pass through /opt/lampp/htdocs/kensaku/
[Wed Mar 20 19:26:28.630277 2013] [rewrite:trace1] [pid 24190] mod_rewrite.c(467): [client 127.0.0.1:52996] 127.0.0.1 - - [www.kensaku.com/sid#982bb28][rid#9a7b118/subreq] [perdir /opt/lampp/htdocs/kensaku/] pass through /opt/lampp/htdocs/kensaku/index.html
[Wed Mar 20 19:26:28.630327 2013] [rewrite:trace1] [pid 24190] mod_rewrite.c(467): [client 127.0.0.1:52996] 127.0.0.1 - - [www.kensaku.com/sid#982bb28][rid#9a7b118/subreq] [perdir /opt/lampp/htdocs/kensaku/] pass through /opt/lampp/htdocs/kensaku/index.html.var
[Wed Mar 20 19:26:28.630360 2013] [rewrite:trace1] [pid 24190] mod_rewrite.c(467): [client 127.0.0.1:52996] 127.0.0.1 - - [www.kensaku.com/sid#982bb28][rid#9a7b118/subreq] [perdir /opt/lampp/htdocs/kensaku/] pass through /opt/lampp/htdocs/kensaku/index.php

编辑2 :以下是vHosts:

# Kensaku Domains
<VirtualHost *:80>
   ServerAdmin admin@kensaku.com
   DocumentRoot /opt/lampp/htdocs/kensaku
   ServerName kensaku.com   
   ServerAlias *.kensaku.com
</VirtualHost>
<VirtualHost *:80>
   ServerAdmin admin@kensaku.com
   DocumentRoot /opt/lampp/htdocs/kensakuapp
   ServerName kensakuapp.com   
   ServerAlias *.kensakuapp.com
</VirtualHost>

2 个答案:

答案 0 :(得分:2)

这里你不需要PHP。

只需抓取RewriteCondRewriteRule中所需的部分,然后在替换中使用它们

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+?)\.kensaku\.com$
RewriteRule ^.*$ http://www.kensakuapp.com/%1/$0 [R,L]

这会在something中的RewriteCond和%1中的RewriteRule中的myapp中捕获$0

答案 1 :(得分:1)

没有看到您的日志我不完全确定,但我的第一个猜测是您需要将传递标记([PT])添加到您的规则中:

RewriteRule ^(.*) /goto.php?username=%1&path=$1 [PT]

除了PT标记之外,我还删除了规则中错误的双^,并且我已明确goto.php的路径(可能需要或不需要但这可能是很好的做法)。关于PT标志,请记住mod_rewrite文档:

  

默认情况下,mod_rewrite将URL映射到文件系统路径。

重写的目标(goto.php?...)不是文件系统路径,因此您需要Apache进一步处理它。 passthrough标志是这样的:

  

强制将生成的URI传递回URL映射引擎,以处理其他URI-to-filename转换器,例如Alias或Redirect。

如果这不能解决问题,请按要求发布日志,我们可以仔细查看