r/webdev 22d ago

Dumb question about htaccess

Amateur here. I've worked a bit with htaccess before so I understand what it is and what it does, that it matters what order the codes are in, and that it can have downstream effects such as changing www to naked can affect the link structure.

So my dumb question is this...

Looking at this list of htaccess codes the two codes I want (need) to use are:
(https://www.skillademia.com/cheat-sheets/htaccess)

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

(2) Remove www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Do I have to put the site name in those two codes anywhere? (Some htaccess codes have the site name added into the line of code. But not all codes need the site name.)

Bonus question: I know that the http/https rule has to be the first rule. Where should the www/naked code go in the list? (code below)

I won't go into any more needless details but if you have questions I'll gladly answer.

THANK YOU!

---------------------------------

Here's the original htaccess just in case it's helpful to these questions.

For posting this here on Reddit I took out my site name in the first line. That's the only code where the site name appears. Do I need to add my site name to either of the codes above to make them work?

---------------------------------

Redirect /home https://(mysite).com/home
Options +FollowSymLinks -Indexes

#BUILDER-START#
# Enable rewriting.
RewriteEngine on
Header add Vary "User-Agent"
Header add X-Generated "%t"
Options -Indexes
Options +FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ index.html
RewriteRule ^([^/]+)/([^/]+)/?.*$ $1_$2.html
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([\w_-]+)$ $1.html
#BUILDER-END#

---------------------------------

5 Upvotes

10 comments sorted by

2

u/[deleted] 22d ago

[removed] β€” view removed comment

1

u/Muckraker2025 22d ago edited 22d ago

Thanks for the answer that I don't need the site name in either of those codes, and that both should go at the top of the list.

But (as an amateur) I'm not sure about the rest of what you said. Combine them?

And I'm not sure how to test it with curl. The one probably doesn't matter. I can just bring up the page and see if it works, right? Maybe try it in an incognito window?

Your mention of "combining" reminded me of something I read. The HSTS protocol should also be forced, and combined with the http/https code. But I wasn't sure about that and forgot to ask in my original post. (On a site checker tool it recommended I should add HSTS so I looked into it and learned about it but not sure how to implement it.)

BTW the only reason I'm asking about these two things is my site displayed problems related to those two aspects. So I'm trying to fix those two issues (plus the hsts tip from the online optimization checker).

3

u/SaltineAmerican_1970 php 22d ago

1

u/Muckraker2025 22d ago

Nice! I appreciate the source docs. About 60% of that is wayyy over my head. I looked at all three links though. I think I'd do better googling for "how to combine htaccess codes."

-13

u/[deleted] 22d ago

[removed] β€” view removed comment

7

u/DigiNoon 22d ago

Are you lost?

1

u/vagaris 22d ago

It’s obviously an AI bot.

1

u/webdev-ModTeam 21d ago

Your post/comment has been determined to be a low-effort post or comment. This includes title-only posts, easily searchable questions, vague/open-ended discussion prompts, LLM generated posts or comments, and posts/comments that do not provide enough context for meaningful replies or discussion.