【问题标题】:Subdomain redirect to a full URL and keeping original subdomain子域重定向到完整的 URL 并保留原始子域
【发布时间】:2014-01-08 13:53:28
【问题描述】:

所以,在开头更形象化:

这是当前的链接结构:

http://example.com/video-slug/wordpress-title-of-the-video-slug

我有一个子域:

http://tv.example.com/

我想要实现的是,当有人点击时:

http://tv.example.com/wordpress-title-of-the-video-slug

发现tv.example.com 等于example.com/video 并打开链接:

http://example.com/video/wordpress-title-of-the-video-slug,但将结构保留在位置栏中:

http://tv.example.com/wordpress-title-of-the-video-slug

【问题讨论】:

  • htaccess 是要走的路!
  • 你想要重写,而不是重定向。
  • 我很高兴说明这一点,我在这里标记了 .htaccess,所以人们会认为我知道该走哪条路,但我需要编码帮助,因为我的 .htaccess 知识等于 1 度开尔文量表。
  • 您是否已经将tv.example.com 指向example.com
  • 我有,但后来我得到 tv.example.com/video/title 而不是 tv.example.com/title...

标签: php wordpress .htaccess redirect


【解决方案1】:

您的.htaccess 文件中需要一个重写规则。根据记忆,这样的事情应该可以工作。

RewriteEngine on 
RewriteRule ^(.*)$ http://example.com/video/$1 [NC]

放置在tv 子域中。

【讨论】:

  • 它将所有内容发送到example.com/video,因此tv.example.com/video-title-slugtv.example.com 相同
  • 太好了,它现在重定向了,现在有办法让它保持原样;现在当我输入tv.example.com/video-title 时,它会打开example.com/video/video-title,但也会在位置栏中对其进行更改,有什么方法可以强制它保持tv.example.com/video-title
  • 我认为这不可能,因为您指向的是不同的子域。
【解决方案2】:

适用于您的问题的更通用的解决方案。 它允许任何http://subdomain.example.com/ 提供位于http://example.com/subdomain/ 的内容,同时保留原始URL(带有子域)。

P 选项(用于代理)用于此目的(将原始 URL 保留在地址栏中),并回答在接受的答案 cmets 中提出的问题(我无法评论...)

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/%1/$1 [L,P]

对于您的具体情况

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^tv\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/video/$1 [L,P]

我现在面临的一个缺点是 PHP $_SERVER URI 设置为翻译后的 URL,而不是原始 URL。但这是另一个问题:)

【讨论】:

    【解决方案3】:

    我认为这是使用 PHP 的一种更简单的方法:

    <?php 
    //add some params
    $_GET['param1'] = value1;
    //the actual page you wish to redirect to
    include "/var/www/page.php";
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      相关资源
      最近更新 更多