简体   繁体   English

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

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

Server by default redirects users who type example.com/folder to example.com/folder/ 默认情况下,服务器会将键入example.com/folder的用户重定向到example.com/folder/

Is there any way to change that (for example when they go to example.com/folder to redirect to otherexample.com/folder/). 有没有办法改变它(例如,当他们转到example.com/folder重定向到otherexample.com/folder/)。 But this has to affect all folders but exclude the root. 但这必须影响所有文件夹但排除根目录。 Also the code should do nothing when they type example.com/folder/. 当他们输入example.com/folder/时,代码也应该什么都不做。

Edit 编辑

Also the code should not affect images and all other files (or better say, if the requested document has file extension), just folders. 此外,代码不应影响图像和所有其他文件(或者更好的说,如果请求的文档具有文件扩展名),只是文件夹。

example: 例:

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

Edit 编辑

And an other edit: Let's say we found the .htaccess code (the 1st answer below). 另一个编辑:假设我们找到了.htaccess代码(下面的第一个答案)。 How do I make it work for all my domain and website expect my blog (example.com/blog) which I don't want to have redirections like whole my website? 我如何使其适用于我的所有域名和网站期望我的博客(example.com/blog)我不希望重定向像整个我的网站?

Server by default redirects users who type example.com/folder to example.com/folder/ 默认情况下,服务器会将键入example.com/folder的用户重定向到example.com/folder/

That's done by mod_dir for several good reasons, all of which are explained in the DirectorySlash documentation. 这是由mod_dir完成的, 原因有几个,所有这些都在DirectorySlash文档中有解释。 You could use DirectorySlash Off to turn that behaviour off, but it is generally NOT a good idea to do that, unless you have VERY good and valid reasons to do so. 您可以使用DirectorySlash Off关闭该行为,但这通常不是一个好主意,除非您有非常好的和有效的理由这样做。

However, having said that, what you've described in your question can be handled by mod_rewrite alone. 但是,话虽如此,您在问题中描述的内容可以仅由mod_rewrite处理。 First, you need to check if the requested resource is a directory. 首先,您需要检查所请求的资源是否是目录。 It needs to be a physical directory inside your DocumentRoot . 它必须是DocumentRoot的物理目录。

RewriteCond   %{DOCUMENT_ROOT}%{REQUEST_URI}  -d

It is vitally important to also apply %{DOCUMENT_ROOT} , otherwise the directory check won't work. 同样应用%{DOCUMENT_ROOT}至关重要,否则目录检查将不起作用。


  • '-d' (is directory) '-d'(是目录)

    Treats the TestString as a pathname and tests whether or not it exists, and is a directory. 将TestString视为路径名并测试它是否存在,并且是一个目录。


Second, you need to ignore all requests to directories that actually do have a trailing slash. 其次,您需要忽略对实际具有尾部斜杠的目录的所有请求。

RewriteCond   %{REQUEST_URI}                  !/$

Now you've filtered down to just the resources you want to redirect to another domain: directories without a trailing slash. 现在,您已经过滤到了要重定向到另一个域的资源:没有尾部斜杠的目录。 Everything else (directories with trailing slashes, images, etc.) is not even in the game at this point. 其他所有(带有斜杠,图像等的目录)此时甚至不在游戏中。 Time for the actual redirect. 实际重定向的时间。

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

You might want to change your mind later on about the redirection, so you should use temporary redirects (aka 302 Found ) instead of permanent ones (aka 301 Moved Permanently ). 您可能希望稍后改变主意关于重定向,因此您应该使用临时重定向(也就是302 Found )而不是永久重定向(又名301 Moved Permanently )。 The last tells mod_rewrite to stop all further processing. last告诉mod_rewrite停止所有进一步处理。

And here's the final product: 这是最终产品:

RewriteEngine On

RewriteCond   %{DOCUMENT_ROOT}%{REQUEST_URI}  -d
RewriteCond   %{REQUEST_URI}                  !/$

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

What about 关于什么

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

Read more here for RewriteEngine method 在这里阅读更多RewriteEngine方法

Hope this is what you need 希望这是你需要的

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

just change directory with your folder name 只需使用您的文件夹名称更改目录

For : www.newdomain.com 对于:www.newdomain.com

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

For : olddomain.com to newdomain.com 对于: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