I know there are plenty of tutorials online and posts here about redirecting HTTP to HTTPS using .htaccess. However, my problem is regarding the URL which will be changed during redirection.

Apache2 should forward "http://localhost/~user/secure_secrets" to https://localhost/~user/secure_secrets". I have apache_2 installed and have public_html being served from users' home directories using userdir module. Now, inside public_html, I have a folder called "secure_secrets", which the URL refers to. For the redirection, I have set .htaccess thus:

# 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]

But it doesn't work. When I type the HTTP url in the browser, it says 404 not found. I think I made a mistake in specifying the HTTP URL in the rewrite URL, but I cannot understand where.

As extra information, my other configurations are:

in /etc/apache2/sites-available/000-default.conf:

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

and, /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
                <LimitExcept GET POST OPTIONS>
                        Order deny,allow
                        Deny from all

I hope someone can help me with this? Thanks in advance!


Your RewriteRule directive is looking for the URL to start with either a forward slash followed by "secure_secrets" or only by "secure_secrets" - you don't mention the "~user" part anywhere.

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

Based on your description, it seems that this is what you want:

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


There are plenty of tutorials online and posts here about redirecting, but I bet none of them aren't advising redirecting to localhost. And it seems like you are doing it. Localhost from the point of view of the browser machine. So the 404 you are seeing is issued by your local web-server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.