1

I'm starting to learn about the Apache .htaccess and I have some issues in my website...

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteCond %{HTTP_HOST} ^example$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.net$
RewriteCond %{REQUEST_URI} !myfolder-public/
RewriteRule (.*) /myfolder-public/$1 [L]

The second RewriteRule exists because I need a different folder (called myfolder-public) to redirect my domain, but when I put this on web browser: example.com without HTTPS the page redirects to: example.net/https:/example.net and I don't know how to fix that, any help?

1
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

This is probably because you are missing the L (Last) flag on the redirect directive. It should read:

RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

(Also, no need to capture the pattern (.*) if you are not using a backreference in the substitution.)

Without the L flag, processing continues through the file and it looks like it's possibly being caught by your later rewrite. But the previous directive has already set the 3xx status, so it ends up rewriting the URL again and then redirecting.

Although I think something may have got lost in your example, as you have switched between .com and .net domains? You are missing the TLD off the first condition in your rewrite and where did the /myfolder-public go in the erroneous redirect?

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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