SSL .htaccess - Simple Q

[亡魂溺海] 提交于 2021-01-05 13:19:51

问题


I truly hate this file... I just spent 6.5 hours trying to figure this out and with my ADHD dyslexia it's just impossible!! I have a domain that I bought for SSL for (currently I Have to wait for the ssl for WWW to kick in but for now the domain without WWW works, for example:

https://tomas.com

The .htaccess I have in root is currently:

RewriteEngine On
RewriteCond %{HTTP_HOST} tomas\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://tomas.com/$1 [R,L]

And the code above does in fact activate SSL which is good. The thing is, I have a few files in root domain but one of them called is:

hello.php (located at: "tomas.com/hello.php")

If I go to: http://tomas.com/hello I want it to display that file (and in address bar it should say: "http://tomas.com/hello"). Before the SSL I had this code below and it worked (but not anymore):

RewriteRule ^([^/]*)/?(.*)$ $1.php

Any idea how the entire .htaccess is supposed to look like? :/

I'm also same time trying to FORCE it to NOT use www (so if they do it should be redirected to a non WWW url) Thank you so much in advance!!!!!!!!!!!!


回答1:


Before the SSL I had this code below and it worked (but not anymore):

RewriteRule ^([^/]*)/?(.*)$ $1.php

Not sure how this "worked" before, it's not complete by itself and does more that simply append a .php extension. You need something to prevent a rewrite loop, since hello.php also matches the pattern ^([^/]*)/?(.*)$.

Try the following instead, after your HTTP to HTTPS redirect.

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]

This first checks that the file with a .php file extension exists before internally rewriting to it.

Alternatively, you could instead just enable MultiViews if you aren't doing any other URL rewriting. For example, at the top of your file:

Options +MultiViews

This uses mod_negotiation to basically enable extensionless URLs for everything!



来源:https://stackoverflow.com/questions/64828783/ssl-htaccess-simple-q

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!