0

Hello and thank you for having a look at my problem. Iam stuck getting my .htaccess file to work. Ive tried a ton of things recommended already here on the stackoverflow forums but none of them seemed to work for me. My .htaccess is located in my apache servers root directory and doesnt seem to do anything. I should mention that my .htaccess file has already been modified by XenForo.

This is my current .htaccess file:

#   Mod_security can interfere with uploading of content such as attachments. If you
#   cannot attach files, remove the "#" from the lines below.
#<IfModule mod_security.c>
#   SecFilterEngine Off
#   SecFilterScanPOST Off
#</IfModule>

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 405 default
ErrorDocument 406 default
ErrorDocument 500 default
ErrorDocument 501 default
ErrorDocument 503 default

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
    RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]

    #   If you are having problems with the rewrite rules, remove the "#" from the
    #   line that begins "RewriteBase" below. You will also have to change the path
    #   of the rewrite to reflect the path to your XenForo installation.
    #RewriteBase /xenforo

    #   This line may be needed to workaround HTTP Basic auth issues when using PHP as a CGI.
    #RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
    RewriteRule ^.*$ index.php [NC,L]
</IfModule>

Ive also tried to put different versions of these lines:

    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
    RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]

at the very top of the file but that hasnt worked either.

My goal is to keep the url non-www and force https at all times. Thanks in advance for helping out.

Max
  • 13
  • 3
  • Why not using Apache main config files? You will have far less problems than using `.htaccess` and a lot of benefits. Also saying you tested a lot of things and nothing worked is not helpful for people to help you you should describe exactly what you try, how you try it, the unexpected results you get and the expected results you wanted instead. You should also apply canonical dichotomy resolution methodology: start with a simple case (one redirection), make sure it works, then add the next one, etc. Trying to solve everything at once is more difficult. – Patrick Mevzek Apr 17 '19 at 22:37
  • Thanks for the response Patrick. As you can probably already tell Iam not that much of an expert regarding apache configs and .htaccess. Ive checked for apache redirect examples and a lot of those sites were immediately recommending using a .htaccess file. So even though I havent postet all of my 50 attempts to get my .htaccess file to work, is there anything particulary wrong with the .htaccess file mentioned above? Thanks in advance. – Max Apr 17 '19 at 22:55
  • This may give you some first hints to troubleshoot: https://stackoverflow.com/questions/215316/how-do-i-troubleshoot-why-my-rewrite-rules-arent-being-applied-by-apache and various examples at https://stackoverflow.com/questions/4083221/how-to-redirect-all-http-requests-to-https. Note as said there that for simple redirections, `Redirect` is far simpler than `mod_rewrite`. – Patrick Mevzek Apr 17 '19 at 23:08
  • Thanks for the advice! I think Iam onto something there solely using apache. Ill have a deeper look into it. – Max Apr 17 '19 at 23:50
  • Have you checked if your dynamic configuration file is evaluated _at all_ ? How? – arkascha Apr 18 '19 at 05:39

1 Answers1

0

This will redirect www to non-www and non-https to https.

#Redirect www and http to https://example.com
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
GuidedHacking
  • 3,628
  • 1
  • 9
  • 59