2

I have an htaccess file for a React app at https://searchglutenfree.com/. I want it to automatically rewrite https://www.searchglutenfree.com/ to https://searchglutenfree.com/ while keeping all the params during the redirection.

I found this great default htaccess template on GitHub (https://gist.github.com/iheartmedia-matt/253ccb6183fdeaa5619f615f2cb5a58b), and getting the www to redirect is the last thing I need. Anyone know what I need to add and where in the file to get the WWW rewrite?

<ifModule mod_rewrite.c>


  #######################################################################
  # GENERAL                                                             #
  #######################################################################

  # Make apache follow sym links to files
  Options +FollowSymLinks
  # If somebody opens a folder, hide all files from the resulting folder list
  IndexIgnore */*


  #######################################################################
  # REWRITING                                                           #
  #######################################################################

  # Enable rewriting
  RewriteEngine On

  # If its not HTTPS
  RewriteCond %{HTTPS} off

  # Comment out the RewriteCond above, and uncomment the RewriteCond below if you're using a load balancer (e.g. CloudFlare) for SSL
  # RewriteCond %{HTTP:X-Forwarded-Proto} !https

  # Redirect to the same URL with https://, ignoring all further rules if this one is in effect
  RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [R,L]

  # If we get to here, it means we are on https://

  # If the file with the specified name in the browser doesn't exist
  RewriteCond %{REQUEST_FILENAME} !-f

  # and the directory with the specified name in the browser doesn't exist
  RewriteCond %{REQUEST_FILENAME} !-d

  # and we are not opening the root already (otherwise we get a redirect loop)
  RewriteCond %{REQUEST_FILENAME} !\/$

  # Rewrite all requests to the root
  RewriteRule ^(.*) /

</ifModule>

<IfModule mod_headers.c>
  # Do not cache sw.js, required for offline-first updates.
  <FilesMatch "sw\.js$">
    Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
    Header set Pragma "no-cache"
  </FilesMatch>
</IfModule>
Tyler Findlay
  • 607
  • 1
  • 4
  • 19
  • See: `https://stackoverflow.com/questions/6515081/htaccess-remove-www-from-url-directories` – hacker315 May 29 '20 at 14:35
  • Thanks @hacker315, I actually inserted this bit of code in a couple of different places in my file and it didn't work for me. Maybe I'm missing something? – Tyler Findlay May 29 '20 at 22:15
  • If you don’t need this to be dynamic regarding the host name, then I would add a Condition that checks if the host name started with `www.` after the one that checks for `%{HTTPS} off`, and add the `[OR]` flag to the former - and then simply hard-code the host name in the substitution URL of the following Rule. – CBroe Jun 03 '20 at 13:42
  • Thanks @CBroe. I'm really bad with htaccess so what I'm looking for is the exact code. I have a bounty up for this so if you answer with the exact change and it works, I'll award you :) – Tyler Findlay Jun 04 '20 at 13:31
  • Added an answer, that should basically work this way. If you leave the rest of the rwriting after the comments I mentioned as-is, everything should work. (Saying _should_ here, because these things aren’t always easy to get a 100% right, without being able to test it on the actual system. Let me know if you encounter any problems with this.) – CBroe Jun 09 '20 at 11:39
  • The current state of the htacess file is here: https://pastebin.com/2DdCXSTH It doesn't properly redirect unless the host name is explicitly pasted in. I am looking for a dynamic URL solution – Tyler Findlay Jun 09 '20 at 14:31

2 Answers2

1

If you don’t need this to be dynamic regarding the host name, then I would add a Condition that checks if the host name started with www. after the one that checks for %{HTTPS} off, and add the [OR] flag to the former - and then simply hard-code the host name in the substitution URL of the following Rule.

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*) https://searchglutenfree.com/$1 [R,L]

If you replace everything between the comments # If its not HTTPS and # If we get to here, it means we are on https:// in your .htaccess you had shown above with that, it should work.

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • The `R` flag on its own will cause a temporary redirect with status code 302, that is good for testing (otherwise your browser will cache the redirects itself, so that can easily fool you, when you see no effect after you made changes.) If everything works as it should, replace that with `R=301` to make it a permanent redirect. – CBroe Jun 09 '20 at 11:39
  • What existing redirects? – CBroe Jun 09 '20 at 13:49
  • (The whole thing should look like this now, https://pastebin.com/U01Af425) – CBroe Jun 09 '20 at 13:51
  • Deleted my last comment. This is 99% there! Is there a way to remove "seachglutenfree.com" and make this a wildcard? I have to copy this into a few sites and would be nice to not have to replace that part. – Tyler Findlay Jun 09 '20 at 13:51
  • Found another issue ... https:// www.searchglutenfree.com does not properly redirect (without the space, added to show the URL that doesn't work) – Tyler Findlay Jun 09 '20 at 13:53
  • If you want this dynamic regarding the host name, then you need a solution that “cuts out” that part from the corresponding environment variable, something like this, https://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www – CBroe Jun 09 '20 at 13:55
  • I don’t see why `https://www.searchglutenfree.com` (use backticks for code, so the URL does not automatically get linked) should not work. Please make sure that you are not getting tricked by old, cached redirects now - clear browser history, or test in a private tab. – CBroe Jun 09 '20 at 13:57
  • I changed your requested code to this: RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*) https://%1/$1 [R=302,L] Now the original redirect doesn't work – Tyler Findlay Jun 09 '20 at 14:07
  • The code from that other StackOverflow isn't working unfortunately. This is why I posted here. I've been trial and error'ing different answers in this existing file and gotten nothing, however this file does everything EXCEPT redirect www to non-www (hence the bounty, cause I knwo this is specialized) – Tyler Findlay Jun 09 '20 at 14:10
  • Well I specifically said, if you _don’t_ need this to be dynamic right upfront … // _“The code from that other SO isn't working unfortunately”_ - in that form, it _can_ only work on its own, because if the host name does not match the pattern `^www\.(.*)$`, then you have no `%1` back reference available to place in the substitution. On its own, this condition, if not matching, will prevent the following rule from doing anything - but if you combine it with the other condition to check for not https, then the whole thing will fail, if the host name did not contain the `www.` part to begin with. – CBroe Jun 09 '20 at 14:19
  • You'll have to pardon me. I am really green with htaccess. I'll try to answer your questions here. Yes, I would like this to be dynamic right upfront if possible. The SO code you linked above did not work when I inserted it where you suggested. – Tyler Findlay Jun 09 '20 at 14:23
  • Here is the current state of things if you're curious: https://pastebin.com/2DdCXSTH – Tyler Findlay Jun 09 '20 at 14:30
0

In order to set up the desired redirect, www.example.com to example.com or vice versa, you must have an A record for each name.

To redirect users from www to a plain, non-www domain, insert this configuration:

in your .htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
aballaci
  • 1,043
  • 8
  • 19
  • I tried this code at line 24 and like 36 of the above htaccess file. At line 26, it will correctly redirect http://searchglutenfree.com, but not htttps://www.searchglutenfree.com. What line should this code be inserted at? If you can tell me which line this works at, I'll gladly give you the bounty. – Tyler Findlay Jun 09 '20 at 13:39