繁体   English   中英

如何将用户重定向到另一个域.htaccess

[英]How to redirect users to another domain .htaccess

默认情况下,服务器会将键入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)我不希望重定向像整个我的网站?

默认情况下,服务器会将键入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]

关于什么

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

在这里阅读更多RewriteEngine方法

希望这是你需要的

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

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

对于: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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM