Apache mod_rewrite 规则没有触发?

Apache mod_rewrite 规则没有触发?

我的服务器上有 Twitch Plays Pokemon Reddit 更新的存档(http://flarn2006.dyndns.org/tppupdates/)。其中一些包含“/u/用户名“,这在 Reddit 上有效,但在我的网站上无效。(我不是指最右边的链接;那些是正确的。)我尝试使用 mod_rewrite 自动重定向这些链接,因此如果它收到“/u/任何事物“其引用站点包含“/tppupdates/”,它会将其重定向到 Reddit。

我的 apache2.conf 中有以下内容:

RewriteEngine on
RewriteCond %{HTTP_REFERER} /tppupdates/
RewriteRule ^/u/(.*)$ http://reddit.com/u/$1 [R=301]

但当我点击其中一个有问题的链接时,它只会显示 404。这是怎么回事?

答案1

好吧,这里有一个更完整的答案:

根据重写指令的位置,重写引擎的工作方式并不完全相同。指令基本上有两种上下文:每个服务器和每个目录。它们如下:

| in apache2.conf files:
|
|   here it is per server
|
|   <VirtualHost>
|
|        per server
|
|        <Directory, Location, File or Proxy>
|
|           per dir !!!
|
|        </Directory, etc>
|   </VirtualHost>

在 .htaccess 中:始终按目录上下文

在每个服务器上下文中,使用以下命令:

RewriteEngine on
RewriteCond %{HTTP_REFERER} /tppupdates/
RewriteRule ^/u/(.*)$ http://reddit.com/u/$1 [R=301]

并且在每个目录上下文中,如果您在 Web 根文件夹中,请使用以下命令,根据您的示例:

RewriteEngine on
RewriteCond %{HTTP_REFERER} /tppupdates/
RewriteRule ^u/(.*)$ http://reddit.com/u/$1 [R=301]

/请注意,RewriteRule 中缺少第一个“ ”。

有了这些规则,您就不需要创建名为 /u 的目录。

Apache 文档中还有更多信息:RewriteRule,匹配的内容技术细节

你确定你想要这个吗RewriteCond

相关内容