在过去的几天里,我已经阅读了很多论坛和StackOverflow的答案,重写了apache.conf中的URL,并使用重写将http转换为https。感谢StackOverflow,我已经想出了如何分别做到这两点。不幸的是,我还没有找到一个可以将两者结合起来的解决方案,同时允许我保持干净的URL外观,同时让我以隐藏的方式将变量传递给页面。
这是我的apache.conf文件的当前目录部分:
<Directory /home/tim/example>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ sort-url.php?rt=$1
</Directory>
我目前将“https://example.com/CA/Los-Angeles”传递给名为“sort-url.php”的文件,在那里我获取“rt”变量,使用“/”将其分解为数组,然后使用变量如$ route_array [0]代表“CA”和$ route_array [1] =“洛杉矶”。现在,用户永远不会看到“sort-url.php”,因此我的网址看起来总是很干净。我希望保持同样的效果,但将http转换为所有网站的https。
如果我将此重写规则替换为其他重写规则,我发现此代码转换为https:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}/sort-url.php?rt=$1 [R,L]
但是,这会导致“sort-url.php?rt = CA / Los-Angeles”显示在URL中。如果我在这样的重写规则中删除“sort-url.php:
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R,L]
它不会知道将所有请求路由到sort-url.php文件,但它看起来像我想要的干净链接https。
我是以错误的方式去做的吗?是否可以在保持干净的URL的同时组合这两个规则?
提前谢谢你, 添
ADDITION - 这是我的虚拟主机:
<VirtualHost XXX.XXX.XX.XXX:12345>
ServerName example.com
Redirect / https://example.com/
</VirtualHost>
<VirtualHost XXX.XXX.XX.XXX:80>
ServerName example.com
Redirect / https://example.com/
</VirtualHost>
<VirtualHost XXX.XXX.XX.XXX:443>
ServerName example.com
DocumentRoot /home/tim/example
SSLEngine on
SSLCertificateFile /etc/ssl/certs/example.com.crt
SSLCertificateKeyFile /etc/ssl/certs/example.key
SSLCertificateChainFile /etc/ssl/certs/sf_bundle.crt
</VirtualHost>
答案 0 :(得分:0)
在DOCUMENT_ROOT/.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ sort-url.php?rt=$1 [L,QSA]
答案 1 :(得分:0)
我建议在vhosts:
中禁止Directory子句(COMMENT)<VirtualHost XXX.XXX.XX.XXX:80>
ServerName example.com
ServerAlias *.example.com
DocumentRoot /home/tim/example
Redirect / https://example.com/
</VirtualHost>
<VirtualHost XXX.XXX.XX.XXX:443>
ServerName example.com
ServerAlias *.example.com
DocumentRoot /home/tim/example
SSLEngine on
SSLCertificateFile /etc/ssl/certs/example.com.crt
SSLCertificateKeyFile /etc/ssl/certs/example.key
SSLCertificateChainFile /etc/ssl/certs/sf_bundle.crt
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /sort-url.php?rt=$1 [L,QSA]
</VirtualHost>
答案 2 :(得分:0)
好吧,看来我的负载均衡器配置(在LB,端口80等上的SSL终止)让我陷入循环,“X-Forwarded-Proto”就是我需要的答案。这是我更新的目录部分:
<Directory /home/tim/example>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=permanent]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ sort-url.php?rt=$1 [L,QSA]
</Directory>