Redirecting non-www to www with .htaccess

If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file:

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

This will redirect any requests to http://my-domain.com to http://www.my-domain.com. There are several benefits from doing that:

  • It will avoid duplicate content in Google
  • It will avoid the possibility of split page rank and/or split link popularity (inbound links).
  • It's nicer, and more consistent.

Note that if your site has already been indexed by Google without the www, this might cause unwanted side effects, like lost of PR. I don't think this would happen, or in any case it would be a temporary issue (we are doing a permanent redirect, 301, so Google should transfer all rankings to the www version). But anyway, use at your own risk!

Something nice about the code above is that you can use it for any website, since it doesn't include the actual domain name.

Redirecting www to non-www

If you want to do the opposite, the code is very similar:

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

In this case we are explicitly typing the domain name. I'm sure it's possible to do it in a generic way, but I haven't had the time to work one out and test it. So remember to change 'my-domain' with your domain name!

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, ortrackback from your own site.

154 comments to “Redirecting non-www to www with .htaccess”

  1. #01 By natalia, 080313 at 21:03

    Would you mind explaining exactly what each part of those two lines does? :)
    Always been curious.

  2. #02 By dense13, 080313 at 22:15

    Sure! The first line sets a condition: only if the condition is true, the second line will be processed. The condition could be 'translated' to: "if the host name doesn't start with www.". The regular expression !^www\. means this:

    ! = not
    ^ = start
    \. = . (the backslash is the escape character, because dots have a special meaning in regular expressions, and therefore must be escaped)

    So !^www\. means "doesn't start with www.".

    The second line is the actual rewrite rule: again it uses regular expressions to match certain urls, and then rewrites them to something else. The first part is the regular expression:

    ^(.*)$

    This means: anything! You already know the ^ sign. The (.*) bit means zero or more characters (the dot means any character, the asterisk means zero or more). The final $ means 'end'.

    Then comes the bit that says how to rewrite the url:

    http://www.%{HTTP_HOST}/$1 [R=301,L]

    %{HTTP_HOST} will be replaced by the host name (i.e. dense13.com).
    $1 references whatever was matched in the regular expression between the brackets, which in this case is everything.
    The [R=301,L] means "inform the user agent that this is a permanent redirect (HTTP 301 code), and don't process any more rewrite rules (if there were any after this one).

    If you're not familiar with regular expressions, this might still look a bit abstract, feel free to ask for more details. :)

本文地址:http://blog.csdn.net/aerchi/article/details/40976651

个人网站:http://www.aerchi.com
<think>好的,我现在要解决用户在CentOS或RHEL系统中使用`sudo service ssh --full-restart`命令时出现的错误。错误提示是`/bin/systemctl: unrecognized option '--full-restart'`,这说明`--full-restart`选项不被识别。我需要先弄清楚为什么会出现这个错误,然后给出正确的解决方法。 首先,我需要回忆一下CentOS/RHEL系统中服务管理的方式。CentOS 7及更高版本已经转向使用systemd作为初始化系统,而不再使用旧的SysVinit。因此,服务管理通常通过`systemctl`命令来进行,比如`systemctl restart sshd`。然而,用户可能仍然习惯使用旧的`service`命令,比如`service sshd restart`,这在systemd系统中实际上会被转发给`systemctl`处理。 用户使用的命令是`sudo service ssh --full-restart`,但错误提示指出`--full-restart`选项不被识别。可能的原因是什么呢?在systemd中,服务管理命令的选项可能与SysVinit不同。`service`命令在systemd环境下可能不支持某些旧选项,比如`--full-restart`。因此,用户需要改用`systemctl`命令的正确选项。 接下来,我需要验证这个猜测。查阅相关文档或手册页,发现`systemctl`的restart命令并没有`--full-restart`这个选项。正确的重启命令应该是`systemctl restart sshd`。所以,用户应该改用这个命令。此外,旧的`service`命令可能仍然兼容,但参数可能需要调整,比如`service sshd restart`。 另外,用户可能混淆了服务名称。在CentOS/RHEL中,SSH服务通常叫做`sshd`,而不是`ssh`。因此,用户可能错误地使用了`ssh`作为服务名,而正确的服务名是`sshd`。这也可能导致问题,因为如果服务不存在,`service`命令会报错。 总结问题原因:用户使用了不兼容的选项`--full-restart`和可能错误的服务名称`ssh`,而正确的命令应该是`systemctl restart sshd`或者`service sshd restart`。 接下来,我需要给出正确的步骤。首先,确认服务名称是否正确,然后使用正确的命令和选项。此外,检查服务状态和配置是否有问题,比如SELinux是否导致服务无法启动,可以参考引用中的关闭SELinux或安装相关软件包的方法,如引用[^1]提到的关闭SELinux或者安装`openstack-selinux`,然后重启httpd服务。虽然这里是SSH服务的问题,但SELinux有时会影响服务启动,所以可能需要检查。 最后,确保用户知道如何正确重启服务,并检查服务状态以确认是否成功。可能还需要提示用户如何启用服务开机启动,如`systemctl enable sshd`,以及如何查看日志排查问题。</think>### 逐步解决方案 在CentOS或RHEL系统中,`service`命令已逐渐被`systemctl`替代。`--full-restart`选项是某些Linux发行版(如Ubuntu)中`ssh`服务的扩展参数,但在CentOS/RHEL中不被支持。以下是正确的操作步骤: --- #### **1. 确认服务名称与操作方式** - **服务名称差异**:CentOS/RHEL中SSH服务名为`sshd`而非`ssh`。 - **正确重启命令**: ```bash sudo systemctl restart sshd ``` 或使用传统`service`命令(自动转发到`systemctl`): ```bash sudo service sshd restart ``` --- #### **2. 检查服务状态** 验证SSH服务是否正常运行: ```bash sudo systemctl status sshd ``` 输出应包含`Active: active (running)`。若服务启动失败,需检查日志: ```bash journalctl -u sshd -xe ``` --- #### **3. 处理SELinux干扰(如需要)** 若SSH服务因SELinux限制无法启动,可尝试临时关闭SELinux: ```bash sudo setenforce 0 ``` 或永久关闭(需编辑`/etc/selinux/config`,设置`SELINUX=disabled`)。 --- #### **4. 确保服务开机自启** ```bash sudo systemctl enable sshd ``` --- ### 命令总结 | 操作 | 命令 | |--------------|-------------------------------| | 重启SSH服务 | `sudo systemctl restart sshd` | | 查看服务状态 | `sudo systemctl status sshd` | | 关闭SELinux | `sudo setenforce 0` | | 开机自启 | `sudo systemctl enable sshd` | --- ### 相关问题 1. **如何查看SSH服务的详细日志?** ```bash journalctl -u sshd ``` 2. **为什么在CentOS中无法使用`--full-restart`选项?** 该选项是Ubuntu/Debian中OpenSSH的扩展功能,CentOS/RHEL的`systemctl`未实现此参数。 3. **如何修改SSH服务的默认端口?** 编辑`/etc/ssh/sshd_config`,修改`Port`字段后重启服务[^2]。 [^1]: 引用[1] [^2]: 引用[2]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值