【问题标题】:How to redirect users to another domain .htaccess如何将用户重定向到另一个域 .htaccess
【发布时间】:2012-12-26 08:46:34
【问题描述】:

服务器默认将键入 example.com/folder 的用户重定向到 example.com/folder/

有什么方法可以改变它(例如,当他们转到 example.com/folder 以重定向到 otherexample.com/folder/ 时)。但这必须影响所有文件夹,但不包括根目录。此外,当他们键入 example.com/folder/ 时,代码不应执行任何操作。

编辑

此外,代码不应影响图像和所有其他文件(或者更好地说,如果请求的文档具有文件扩展名),只影响文件夹。

示例:

should affect : example.com/folder
              : example.com/folder/folder

but should not : example.com
               : example.com/forum (but only forum, only this folder)
               : example.com/folder/
               : example.com/folder/.
               : example.com/folder/folder/
               : example.com/image.png
               : example.com/something.something
               : example.com/something.
               : example.com/.something

编辑

还有另一个编辑: 假设我们找到了 .htaccess 代码(下面的第一个答案)。如何使其适用于我的所有域和网站(除了我的博客 (example.com/blog)),我不想像整个网站那样进行重定向?

【问题讨论】:

    标签: php html apache .htaccess mod-rewrite


    【解决方案1】:

    默认情况下,服务器会将键入 example.com/folder 的用户重定向到 example.com/folder/

    mod_dir 这样做有几个很好的理由,所有这些都在DirectorySlash 文档中进行了解释。您可以使用DirectorySlash Off 来关闭该行为,但这样做通常不是一个好主意,除非您有非常好的和正当的理由这样做。

    但是,话虽如此,您在问题中描述的内容可以由mod_rewrite 单独处理。首先,您需要检查请求的资源是否为目录。它必须是DocumentRoot 中的物理目录。

    RewriteCond   %{DOCUMENT_ROOT}%{REQUEST_URI}  -d
    

    同时申请%{DOCUMENT_ROOT}也很重要,否则目录检查将不起作用。


    • '-d'(是目录)

      把TestString当作路径名,测试它是否存在,是否是一个目录。


    其次,您需要忽略对实际上有斜杠的目录的所有请求。

    RewriteCond   %{REQUEST_URI}                  !/$
    

    现在您已经过滤到您想要重定向到另一个域的资源:没有尾部斜杠的目录。其他所有内容(带有斜杠的目录、图像等)此时甚至都不在游戏中。实际重定向的时间。

    RewriteRule   ^   http://other.com%{REQUEST_URI}/ [redirect=temp,last]
    

    您可能希望稍后对重定向改变主意,因此您应该使用临时重定向(又名302 Found)而不是永久重定向(又名301 Moved Permanently)。 last 告诉 mod_rewrite 停止所有进一步的处理。

    这是最终产品:

    RewriteEngine On
    
    RewriteCond   %{DOCUMENT_ROOT}%{REQUEST_URI}  -d
    RewriteCond   %{REQUEST_URI}                  !/$
    
    RewriteRule   ^   http://other.com%{REQUEST_URI}/ [redirect=temp,last]
    

    【讨论】:

    • 如果你让我问你还有一个问题。是否有任何代码不允许此代码影响我位于 example.com/blog 的博客。
    • 是的,添加RewriteCond %{REQUEST_URI} !^/blog。那么,赏金呢?
    【解决方案2】:

    怎么样

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://otherexample.com/$1/ [L,R=301]
    

    【讨论】:

    • 但我不希望它重定向图像和其他东西,只是文件夹
    • 所以,我的意思是,它完成了这项工作,但它做得更多是不好的。如果可以,请使其更具体,仅限文件夹。
    【解决方案3】:
    # This allows you to redirect your entire website to any other domain
    Redirect 301 / http://mt-example.com/
    
    # This allows you to redirect your entire website to any other domain
    Redirect 302 / http://mt-example.com/
    
    # This allows you to redirect index.html to a specific subfolder
    Redirect /index.html http://example.com/newdirectory/
    
    # Redirect old file path to new file path
    Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html
    
    # Provide Specific Index Page (Set the default handler)
    DirectoryIndex index.html
    

    Read more here for RewriteEngine method

    【讨论】:

      【解决方案4】:

      希望这是你需要的

      Redirect 301 /directory http://www.newdomain.com
      

      只需使用您的文件夹名称更改目录

      【讨论】:

      • 如果文件夹名称相同,则会进行无限重定向循环
      【解决方案5】:

      适用于:www.newdomain.com

      RewriteEngine On
      RewriteBase /
      RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
      RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]
      

      对于:olddomain.com 到 newdomain.com

      RewriteEngine On
      RewriteBase /
      RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
      RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
      

      【讨论】:

        猜你喜欢
        • 2020-05-04
        • 1970-01-01
        • 1970-01-01
        • 2014-03-16
        • 1970-01-01
        • 1970-01-01
        • 2017-05-18
        • 2011-06-29
        • 1970-01-01
        相关资源
        最近更新 更多