使用.htaccess和mod_rewrite将HTTPredirect到HTTP

我知道有大量的在线教程,并在这里发表有关使用.htaccess将HTTPredirect到HTTPS的信息。 但是,我的问题是关于在redirect期间将被改变的URL。

Apache2应该将“ http:// localhost /〜user / secure_secrets ” 转发到https:// localhost /〜user / secure_secrets “我已经安装了apache_2,并且使用userdir模块从用户的主目录中提供了public_html现在,在public_html ,我有一个名为“secure_secrets”的文件夹,这个URL指的是redirect,因此我设置了.htaccess:

# Enable rewrite RewriteEngine on # Check if connection is not already HTTPS RewriteCond %{HTTPS} !=on RewriteRule ^/?secure_secrets/(.*) https://localhost/~user/secure_secrets/$1 [R,L] 

但它不起作用。 当我在浏览器中inputHTTPurl时,说404找不到。 我想我在重写URL中指定HTTP URL时犯了一个错误,但是我不明白在哪里。

作为额外的信息,我的其他configuration是:

在/etc/apache2/sites-available/000-default.conf中:

 <Directory "/var/www/html"> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> 

和/etc/apache2/mods-enabled/userdir.conf:

  <IfModule mod_userdir.c> UserDir public_html UserDir disabled root <Directory /home/*/public_html> AllowOverride All Options MultiViews Indexes SymLinksIfOwnerMatch <Limit GET POST OPTIONS> Order allow,deny Allow from all </Limit> <LimitExcept GET POST OPTIONS> Order deny,allow Deny from all </LimitExcept> </Directory> </IfModule> 

我希望有人能帮助我呢? 提前致谢!

您的RewriteRule指令正在寻找以正斜线开头,后跟“secure_secrets”或仅由“secure_secrets”开头的URL – 您不要在任何地方提及“〜user”部分。

RewriteRule ^/?secure_secrets/(.*) https://localhost/~user/secure_secrets/$1 [R,L]

根据你的描述,似乎这是你想要的:

RewriteRule ^/~user/secure_secrets/(.*) https://localhost/~user/secure_secrets/$1 [R,L]

网上有大量的教程,并在这里发布关于redirect,但我敢打赌,他们都不build议redirect到本地主机。 看来你正在这样做。 从浏览器的angular度看本地主机。 所以你看到的404是由你的本地networking服务器发出的。