0

我正在本地项目中进行一些测试(使用 vHosts)。我设法创建了一个 RewriteRule 以重定向something.kensaku.comwww.kensakuapp.com/something,但我想重定向something.kensaku.com/myappwww.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:这里是虚拟主机:

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

2 回答 2

2

这里不需要 PHP。

只需在 a 中捕获所需的部分RewriteCondRewriteRule在替换中使用它们

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

something这在 RewriteCond in%1myappRewriteRule in 中捕获$0

于 2013-03-18T21:26:27.033 回答
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 到文件名的转换器,例如 Alias 或 Redirect。

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

于 2013-03-18T19:53:19.293 回答