在 Rails 应用程序中重定向用户后,由于某种原因,域后的斜线丢失了。生成的 URL 无效,我不得不手动更正它们。该问题仅发生在子域上。在另一个主域(同一台服务器)上,一切正常。
例如,退出后,网站将定向到https://www.sub.domain.com登录/而不是https://www.sub.domain.com/login
我怀疑该问题与虚拟主机设置有关,但我不确定。以下是损坏和正常运行的虚拟主机:
子域名损坏
<VirtualHost *:80>
ServerName www.sub.domain.com
ServerAlias sub.domain.com
Redirect permanent / https://www.sub.domain.com
</VirtualHost>
<VirtualHost *:443>
ServerAdmin [email protected]
ServerName www.sub.domain.com
ServerAlias sub.domain.com
RailsEnv production
# SSL Engine Switch
SSLEngine on
# SSL Cipher Suite:
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
# Server Certificate
SSLCertificateFile /path/to/server.crt
# Server Private Key
SSLCertificateKeyFile /path/to/server.key
# Set header to indentify https requests for Mongrel
RequestHeader set X_FORWARDED_PROTO "https"
BrowserMatch ".*MSIE.*" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
DocumentRoot /home/usr/www/www.sub.domain.com/current/public/
<Directory "/home/usr/www/www.sub.domain.com/current/public">
AllowOverride all
Allow from all
Options -MultiViews
</Directory>
工作主域
<VirtualHost *:80>
ServerName www.diffdomain.com
ServerAlias diffdomain.com
Redirect permanent / https://www.diffdomain.com
</VirtualHost>
<VirtualHost *:443>
ServerAdmin [email protected]
ServerName www.diffdomain.com
ServerAlias diffdomain.com
ServerAlias *.diffdomain.com
RailsEnv production
# SSL Engine Switch
SSLEngine on
# SSL Cipher Suite:
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
# Server Certificate
SSLCertificateFile /path/to/server.crt
# Server Private Key
SSLCertificateKeyFile /path/to/server.key
# Set header to indentify https requests for Mongrel
RequestHeader set X_FORWARDED_PROTO "https"
BrowserMatch ".*MSIE.*" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
DocumentRoot /home/usr/www/www.diffdomain.com/current/public/
<Directory "/home/usr/www/www.diffdomain.com/current/public">
AllowOverride all
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
如果我可以提供任何其他信息来帮助确定这里出了什么问题,请告诉我。
更新 尝试在重定向命令中添加尾随斜杠,但仍然没有成功。
答案1
我是相当确定你需要在重定向命令上使用斜线:
Redirect permanent / https://www.sub.domain.com/
答案2
向另一位开发人员询问了这个问题,他是这样修复的:
我为你新建了一个 vhost 文件。在之前的 vhost 配置中,服务器名称和服务器别名存在错误。我删除了“www”。我还将其添加到config.force_ssl = true
了 environment/production.rb
<VirtualHost *:80>
ServerName sub.domain.com
ServerAlias sub.domain.com www.sub.domain.com
Redirect permanent / https://sub.domain.com
</VirtualHost>
<VirtualHost *:443>
ServerAdmin [email protected]
ServerName sub.domain.com
ServerAlias sub.domain.com
RailsEnv production
RailsBaseURI /
# SSL Engine Switch
SSLEngine on
# SSL Cipher Suite:
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
# Server Certificate
SSLCertificateFile /path/to/server.crt
# Server Private Key
SSLCertificateKeyFile /path/to/server.key
# Set header to indentify https requests for Mongrel
RequestHeader set X_FORWARDED_PROTO "https"
BrowserMatch ".*MSIE.*" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
DocumentRoot /home/usr/www/www.sub.domain.com/current/public/
<Directory "/home/usr/www/www.sub.domain.com/current/public">
AllowOverride all
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>