安装ssl证书

时间:2016-04-25 17:01:51

标签: apache .htaccess mod-rewrite ssl content-management-system

我编写了一个内容管理系统,该系统使用重写规则将网址映射到controlleractionargument查询字符串。

我使用两个.htaccess文件。一个是在我网站的根目录中。这个请求将所有请求转发到子目录,这取决于用于请求的域名:

SetEnv HTTP_MOD_REWRITE On
RewriteEngine on

# mapp requests that don't start with www to https://www

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

# BEGIN Domain to folder mapping

########################

# pointing example.com to subfolder 'example'
ReWriteCond %{HTTP_HOST} (www\.)?example.com
ReWriteCond %{REQUEST_URI} !example/
ReWriteRule ^(.*)$ example/$1 [L]

# END Domain to folder mapping

将请求映射到实际查询字符串的第二个.htaccess文件位于第一个重写步骤中映射到的子目录中:

SetEnv HTTP_MOD_REWRITE On
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ([^/]*)([/]*)([^/]*)([/]*)(.*) index.php?controller=$1&action=$3&args=$3 [L,QSA]

在安装我的ssl证书之前,所有这些的结果是一个看起来像这样的请求:

example.com/hello/foo/123

将映射到:

http://www.example.com?controller=hello&action=foo&args=123

现在我只是被发送到索引文件。如果我输入实际的查询字符串,我会得到正确的结果。

我查看了服务器的重写日志,看起来还有一个额外的重写步骤,在我无法弄清楚原因的情况下执行。我认为这是重写日志中的相关内容:

[rewrite:trace3] [www.example.com/sid#8021c1788][rid#807418748/initial/redir#1] [perdir /fs6c/example/public/example/] strip per-dir prefix: /fs6c/example/public/example/hello -> hello
[rewrite:trace3] [www.example.com/sid#8021c1788][rid#807418748/initial/redir#1] [perdir /fs6c/example/public/example/] applying pattern '([^/]*)([/]*)([^/]*)([/]*)(.*)' to uri 'hello'
[rewrite:trace2] [www.example.com/sid#8021c1788][rid#807418748/initial/redir#1] [perdir /fs6c/example/public/example/] rewrite 'hello' -> 'index.php?controller=hello&action=&args='
[rewrite:trace3] [www.example.com/sid#8021c1788][rid#807418748/initial/redir#1] split uri=index.php?controller=hello&action=&args= -> uri=index.php, args=controller=hello&action=&args=

我假设[L]旗帜是罪魁祸首,但我不明白为什么在切换到ssl之前这不是问题。

我正在使用共享托管服务,他们必须为我安装证书,因为他们不会向用户公开服务器的必要部分。

1 个答案:

答案 0 :(得分:0)

我仍然不知道为什么会发生这种情况,但我能够找到一种方法使其与ssl证书一起使用。这些是位于根目录中的htaccess文件中的相关重写规则:

# for existing files, redirect to subdirectory
ReWriteCond %{HTTP_HOST} ^(www\.)?example.com
ReWriteCond %{REQUEST_URI} !example/
RewriteCond %{DOCUMENT_ROOT}/example/$1 -f
ReWriteRule ^(.*) example/$1 [L]

# for non-existing files, map to index.php with CMS arguments
ReWriteCond %{HTTP_HOST} ^(www\.)?example.com

# file exists, but is index.php
ReWriteCond %{REQUEST_URI} index.php [OR]

# file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f

ReWriteRule ^([^/]*)([/]*)([^/]*)([/]*)(.*) example/index.php?controller=$1&action=$3&args=$5 [L,QSA]

我删除了第二个htaccess文件。

我希望这有助于将来的某些人。托管服务提供商是NearlyFreeSpeech。如果有人知道为什么我的旧规则不起作用,我真的很感激知道!

相关问题