---
title: "apache的斜杠问题"
author: "jishublog"
date: "2013-09-22T17:51:00Z"
tags: []
slug: "apache"
draft: false
lastmod: "2013-09-22T19:44:09Z"
id: 1945535
url: "https://jishublog.iteye.com/blog/1945535"
---

```
APACHE默认情况下，网页目录的最后必须加入斜杠“/",比如

可以浏览http://www.example.com/abc/,但是不能浏览http://www.example.com/abc,就是说浏览目录时最后必须加/ 改一下设置文件,在httpd.conf里，找到

UseCanonicalName On

把On修改为Off就可：
UseCanonicalName Off
就可以了

当然也可用mod_rewrite来解决，首先要确认你的apache里已经安装了这module,且你的httpd.conf里起用了rewrite,才可以调用.htaccess

# This controls which options the .htaccess files in directories can
# override. Can also be "All", or any combination of "Options", "FileInfo",
# "AuthConfig", and "Limit"
#
    AllowOverride all

当向服务器请求的是
http://www.example.com/faq而不是http://www.example.com/faq/时
，服务器就会去查找一个叫faq的文件，而实际上faq是目录，所以就会报错，无法自动跳转。

解决方法：

编辑用户网站主目录下的.htaccess文件：
在DocumentRoot下，建立.htaccess文件，写进如下内容：
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ http://www.example.com/$1/ [R=301]

这样，服务器就会自动添加后缀斜杠，实现自动跳转。

Apache中ErrorDocument文件的设置问题
在httpd.conf中设置 ErrorDocument 404 /missing.html 
但其中的内容死活不能用，原来是IE的一个BUG，错误文件需要大于512字节才能正常被显示。
教训： 
1.手册是好东西，一定要细心去看。 
2.很多情况下应该换个角度去思考问题。在本例中，把IE中的 "Show Friendly HTTP Error Messages"（ie里设置“显示友好的http错误信息）打开后，就一切迎刃而解了。

使访问http的转向到https

RewriteEngine On
# For webmail
RewriteCond %{HTTP_HOST} ^webmail\.lwkp\.com\.cn
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} ^80$
RewriteRule (.*) https://www.xxx.com[L,R]

# For IP
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^xx\.xx\.xxx9\.[1-9]*$
RewriteRule (.*) https://%{HTTP_HOST}/

```