I'm using the following to redirect http to https in my root directory htaccess file

<IfModule mod_rewrite.c>

  RewriteEngine On

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

</IfModule>

But this seems not to work if I access directly sub directories () unless I place this mod_rewrite in an htaccess file in all my sub drectories.

Anyone knows a better way to do this? Just in my root htaccess file?

Dani AI

Generated

— the redirect in a root .htaccess normally covers requests for subdirectories, so having to copy it into every subfolder points to an environmental or override issue rather than a syntax problem. ’s point about server config is relevant: on shared hosting the server can be set so .htaccess behavior differs, or subfolders may be served from different places (Alias, symlink, separate vhost), or a child .htaccess may be cancelling/overriding the parent rules.

Quick, practical checks (run from the site root or ask support to run them):

# find any child .htaccess files
find . -name .htaccess -print

# see raw response headers and any redirect
curl -I http://example.com/sub-directory/

# verbose request to inspect handshake / headers
curl -v http://example.com/sub-directory/

Look for another .htaccess, a server-side redirect in an index file, or a cached 301 in the browser. Use a 302 while testing so browsers don’t cache the result.

If the host sits behind a proxy/CDN (Cloudflare, load balancer) the usual %{HTTPS} check can be false; check X-Forwarded-Proto instead. Example (place in root .htaccess while testing):

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://example.com%{REQUEST_URI} [R=302,L]

Switch to R=301 after confirming behavior. Using a fixed hostname (example.com) avoids trusting an arbitrary Host header.

Longer-term fixes: ask hosting to implement the redirect in the vhost (server-level redirect or a RedirectMatch) or to enable the correct AllowOverride behavior so one root .htaccess can control all subpaths. If issues persist after these checks, server support can confirm aliasing, proxy headers, or alternate document roots for specific subfolders.

Recommended Answers

All 3 Replies

Hi!

If you have access to the configuration file for the virtualhost then got the <Directory> and set AllowOverride to All:

<Directory /path/to/www/public>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
</Directory>

Remember to adjust the options to your needs, the above example will show directory contents and so on... If you can choose the web server, then try Caddy, the community version is free and works good. It supports HTTP/2 and LetsEncrypt by default: https://caddyserver.com/

Thanks, Cereal!

It's a shared hosting, so I don't have access to a server config file. Caddy looks interesting. Especially the HTTP/2.

It's a shared hosting, , so I don't have access to a server config file

Okay, then you may want to ask to their support. The AllowOverride property is not always enabled for security and performance purposes, as Apache needs to scan the contents of each .htaccess along the folder tree and reset the rules accordingly.

virtualhost then got the <Directory>

oh my «got» o_o

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.