htaccess - HTTP到HTTPS和.HTML到/

时间:2015-12-06 05:08:55

标签: .htaccess http https

我需要.htaccess的帮助:

强制http://https://

强制.html/

到目前为止我所拥有的:

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

3 个答案:

答案 0 :(得分:1)

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
我之前使用过这两种产品,但我还没有将它们一起使用过。希望这会有所帮助。

(编辑) 实际上FORCE扩展看起来像目录:

RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=302,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]

http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ http://www.inmotionhosting.com/support/website/ssl/how-to-force-https-using-the-htaccess-file

答案 1 :(得分:1)

Options -MultiViews

RewriteEngine On
#1 This line checks if the https is off
RewriteCond %{HTTPS} ^off$
#then, redirect to https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [NC,L,R]
#2 this line checks if the request is /file.html
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
#then redirect /file.html to /file
RewriteRule ^ /%1 [NC,L,R]

#3 if the request is not for dir
RewriteCond %{REQUEST_FILENAME} !-d
#and the request is an existing filename
RewriteCond %{REQUEST_FILENAME}.html -f
#then rewrite /file to /file.html
RewriteRule ^([^/]+)/?$ $1.html [NC,L]

在上面的示例中,当原始方案是HTTP时,满足第一个条件,然后处理规则。 HTTP转到HTTPS。第一轮重写处理在这里结束。

在第二轮中,mod_rewrite接受URI /file.html并且规则将其重定向到/ file,因为/ file在目录中不存在,所以我们需要将其重写为原始文件#3 ...

答案 2 :(得分:1)

首先,确保启用mod-rewrite 然后,将此代码放入htaccess(应该在文档根文件夹中)

Options -MultiViews

RewriteEngine On
RewriteBase /

# Redirect http urls to https equivalent
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://{HTTP_HOST}/$1 [R=301,L]

# Redirect existing /path/file.html to /path/file/
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/(.+?)\.html\s [NC]
RewriteRule ^ %1/ [R=301,L]

# Internally rewrite back /path/file/ to /path/file.html (if existing)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+)/$ $1.html [L]

警告

  1. 确保提供与https(apache ssl block configuration)
  2. 的http相同的文档根目录
  3. 请注意,这会创建虚拟目录(通过添加尾随 斜线)这可能搞乱你的HTML资源(如果你正在使用 相对路径而不是绝对路径)。如果是,请使用绝对路径 代替