你如何实现简单的网站重定向?

时间:2021-10-13 14:30:51

If you go to www.codinghorror.com, it is automatically redirected to www.codinghorror.com/blog

如果您访问www.codinghorror.com,它会自动重定向到www.codinghorror.com/blog

How does one re-direct to the "/blog/"?

如何重新指向“/ blog /”?

I'm using Dreamhost Shared Hosting, so my options of configuring the server are limited.

我正在使用Dreamhost共享主机,因此我配置服务器的选项有限。

4 个解决方案

#1


This would be a good opportunity to teach yourself about HTTP status codes. Read the status code definitions section of the HTTP 1.1 spec; section 10.3, "Redirection 3xx," covers the status codes pertinent to your question. Knowing the HTTP 3xx codes means that you can send a response that has exactly the semantics that you intend. For example, if you intend for the redirect to always occur, you could use HTTP 301, "Moved Permanently." In theory, a client could use the HTTP 301 response as a signal to store the new URL value in place of the old one.

这将是一个很好的机会,可以自学HTTP状态代码。阅读HTTP 1.1规范的状态代码定义部分;第10.3节“重定向3xx”包含与您的问题相关的状态代码。了解HTTP 3xx代码意味着您可以发送具有您想要的语义的响应。例如,如果您打算始终进行重定向,则可以使用HTTP 301,“永久移动”。理论上,客户端可以使用HTTP 301响应作为信号来存储新的URL值来代替旧的URL值。

Your configuration options at Dreamhost are not as limited as you would think, because you can specify many Apache web server configuration directives in a hidden .htaccess file. This file should be placed in a web document directory; using your example, you would place it in the root web document directory, /home/midas/codinghorror/, though .htaccess files can be placed in any directory served by Apache. (Remember to include the leading dot in the filename.) Its contents would be either the mod_alias example or the mod_rewrite example already mentioned.

您在Dreamhost上的配置选项没有您想象的那么有限,因为您可以在隐藏的.htaccess文件中指定许多Apache Web服务器配置指令。该文件应放在Web文档目录中;使用您的示例,您可以将它放在根Web文档目录/ home / midas / codinghorror /中,但.htaccess文件可以放在Apache提供的任何目录中。 (请记住在文件名中包含前导点。)其内容可以是mod_alias示例,也可以是已经提到的mod_rewrite示例。

Note that mod_alias's RedirectPermanent directive will send an HTTP 301 status code for you. If you wanted to use mod_rewrite to do this, you could specify the status code:

请注意,mod_alias的RedirectPermanent指令将为您发送HTTP 301状态代码。如果您想使用mod_rewrite执行此操作,则可以指定状态代码:

RewriteEngine on
RewriteRule ^$ /blog/ [R=301]

If you use [R] without a code, then HTTP 302 ("Moved Temporarily") is used.

如果在没有代码的情况下使用[R],则使用HTTP 302(“暂时移动”)。

Since PHP is also available to you, that's also an option, though it's possible that the above options using .htaccess are faster. You would place a file called index.php in /home/midas/codinghorror/ and use the code given by Jeremy Ruten above. Again, you can specify a status code in the third argument to header():

由于PHP也可供您使用,这也是一种选择,尽管使用.htaccess的上述选项可能更快。您可以在/ home / midas / codinghorror /中放置一个名为index.php的文件,并使用上面的Jeremy Ruten给出的代码。同样,您可以在header()的第三个参数中指定状态代码:

<?php
header('Location: http://www.codinghorror.com/blog', TRUE, 301);
?>

Otherwise, using PHP's header() function with 'Location' defaults to sending an HTTP 302 status response.

否则,使用带有'Location'的PHP的header()函数默认发送HTTP 302状态响应。

#2


Another option is using Apache's mod_alias to do a permanent redirect (in either .htaccess or httpd.conf):

另一种选择是使用Apache的mod_alias进行永久重定向(在.htaccess或httpd.conf中):

RedirectPermanent / /blog

There are probably as many ways as there are servers and programming language (probably even more then that). If you tell us what specific technology you use it can probably help to give you a more specific answer.

服务器和编程语言可能有很多种方式(可能还有更多)。如果您告诉我们您使用的具体技术,它可能有助于为您提供更具体的答案。

#3


Assuming Apache with mod_rewrite and context permissions allowing its use, you put this in virtual host configuration or a .htaccess:

假设Apache使用mod_rewrite和允许其使用的上下文权限,则将其置于虚拟主机配置或.htaccess中:

RewriteEngine on
RewriteRule ^$ /blog/

#4


One way is by sending a 'Location' header to the client. Here's a PHP example:

一种方法是向客户端发送“位置”标题。这是一个PHP示例:

<?php

header('Location: http://www.codinghorror.com/blog');

?>

#1


This would be a good opportunity to teach yourself about HTTP status codes. Read the status code definitions section of the HTTP 1.1 spec; section 10.3, "Redirection 3xx," covers the status codes pertinent to your question. Knowing the HTTP 3xx codes means that you can send a response that has exactly the semantics that you intend. For example, if you intend for the redirect to always occur, you could use HTTP 301, "Moved Permanently." In theory, a client could use the HTTP 301 response as a signal to store the new URL value in place of the old one.

这将是一个很好的机会,可以自学HTTP状态代码。阅读HTTP 1.1规范的状态代码定义部分;第10.3节“重定向3xx”包含与您的问题相关的状态代码。了解HTTP 3xx代码意味着您可以发送具有您想要的语义的响应。例如,如果您打算始终进行重定向,则可以使用HTTP 301,“永久移动”。理论上,客户端可以使用HTTP 301响应作为信号来存储新的URL值来代替旧的URL值。

Your configuration options at Dreamhost are not as limited as you would think, because you can specify many Apache web server configuration directives in a hidden .htaccess file. This file should be placed in a web document directory; using your example, you would place it in the root web document directory, /home/midas/codinghorror/, though .htaccess files can be placed in any directory served by Apache. (Remember to include the leading dot in the filename.) Its contents would be either the mod_alias example or the mod_rewrite example already mentioned.

您在Dreamhost上的配置选项没有您想象的那么有限,因为您可以在隐藏的.htaccess文件中指定许多Apache Web服务器配置指令。该文件应放在Web文档目录中;使用您的示例,您可以将它放在根Web文档目录/ home / midas / codinghorror /中,但.htaccess文件可以放在Apache提供的任何目录中。 (请记住在文件名中包含前导点。)其内容可以是mod_alias示例,也可以是已经提到的mod_rewrite示例。

Note that mod_alias's RedirectPermanent directive will send an HTTP 301 status code for you. If you wanted to use mod_rewrite to do this, you could specify the status code:

请注意,mod_alias的RedirectPermanent指令将为您发送HTTP 301状态代码。如果您想使用mod_rewrite执行此操作,则可以指定状态代码:

RewriteEngine on
RewriteRule ^$ /blog/ [R=301]

If you use [R] without a code, then HTTP 302 ("Moved Temporarily") is used.

如果在没有代码的情况下使用[R],则使用HTTP 302(“暂时移动”)。

Since PHP is also available to you, that's also an option, though it's possible that the above options using .htaccess are faster. You would place a file called index.php in /home/midas/codinghorror/ and use the code given by Jeremy Ruten above. Again, you can specify a status code in the third argument to header():

由于PHP也可供您使用,这也是一种选择,尽管使用.htaccess的上述选项可能更快。您可以在/ home / midas / codinghorror /中放置一个名为index.php的文件,并使用上面的Jeremy Ruten给出的代码。同样,您可以在header()的第三个参数中指定状态代码:

<?php
header('Location: http://www.codinghorror.com/blog', TRUE, 301);
?>

Otherwise, using PHP's header() function with 'Location' defaults to sending an HTTP 302 status response.

否则,使用带有'Location'的PHP的header()函数默认发送HTTP 302状态响应。

#2


Another option is using Apache's mod_alias to do a permanent redirect (in either .htaccess or httpd.conf):

另一种选择是使用Apache的mod_alias进行永久重定向(在.htaccess或httpd.conf中):

RedirectPermanent / /blog

There are probably as many ways as there are servers and programming language (probably even more then that). If you tell us what specific technology you use it can probably help to give you a more specific answer.

服务器和编程语言可能有很多种方式(可能还有更多)。如果您告诉我们您使用的具体技术,它可能有助于为您提供更具体的答案。

#3


Assuming Apache with mod_rewrite and context permissions allowing its use, you put this in virtual host configuration or a .htaccess:

假设Apache使用mod_rewrite和允许其使用的上下文权限,则将其置于虚拟主机配置或.htaccess中:

RewriteEngine on
RewriteRule ^$ /blog/

#4


One way is by sending a 'Location' header to the client. Here's a PHP example:

一种方法是向客户端发送“位置”标题。这是一个PHP示例:

<?php

header('Location: http://www.codinghorror.com/blog');

?>