Jump to content

Recommended Posts

Hello.

To redirect "non-www to www" and "non-https to https," I found this efficient three-liner:

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

I would also like to redirect /index.html to root, what's the best way? (By "best," I mean fewest redirects and least strain on Apache.)

Examples:
http://example.com/data.html redirects to https://www.example.com/data.html
http://example.com/index.html redirects to https://www.example.com/

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/308169-3-redirects-https-www-and-root/
Share on other sites

I kinda feel like being tricky.

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [OR]
RewriteCond %{REQUEST_URI} ^/index\.html(\?|$)
RewriteRule ^index\.html$|(.*) https://www.example.com/$1 [L,R=301]

 

Requinix, thank you very much! That is fantastic!!

I know it's a week later.... but I purposely didn't check back for a few days because I wanted to see what I could come up with all by myself :-)

The goal was twofold
1. No more than one redirect.
2. Least amount of strain on Apache.

Here's what I came up with:

RewriteCond %{REQUEST_URI} /index\.html$
RewriteRule (.*?)(index\.html)?$ https://www.example.com$1 [NE,R=301,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

And just now I saw your reply. My code has 5 lines. Your code has 4 lines. You win! You beat me!

I'll use your code, less lines of code means less work for the Apache server.

Thank you!!!

It also means one less redirect. Your method can result in two: first to remove the index.html, second to use HTTPS with the right subdomain.

Also yours doesn't allow for query strings on index.html.

And a slight edit to mine:

RewriteRule ^/?index\.html$|/?(.*) https://www.example.com/$1 [L,R=301]

 

I just discovered a problem though.

The redirects work most of the time.... for example:

http://example.com/merchandise.html correctly redirects to
https://www.example.com/merchandise.html

However,

http://example.com/blog redirects to
http://example.com/blog/ (just adds a trailing slash but no https)

-and-

Erratic results using same browser, clearing browser's cache inbetween tests:

example.com/blog sometimes redirects to
https://www.example.com (omits the subdirectory)

http://example.com/blog sometimes redirects to
example.com/blog (omits the 'http' entirely, doesn't add trailing slash)

example.com/blog sometimes redirects to
example.com/blog./ (just adds a trailing slash, but no https)

hmmm....

edit: (update):

Because I no longer need the "index.html to /", this is the current code:

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

However, the problem is this code forgets to force the "www" when inputting a URL like "example.com/directory"

example.com/directory resolves to https://example.com/directory/ instead of the desired "https://www.example.com/directory/"

No, nothing changed. In about an hour I'll be at my other computer, and I'll try a scientific test: I'll place the same code on 2 different webhost's servers using fresh blank .htaccess files, and newly created "test" subdirectories with a "hello world" index file in each one.

Then I'll try again and see if http://example.com/testdirectory redirects to http://www.example.com/testdirectory/

I'll come back here and post the results.

Thank you.

No sense.... just makes no sense.... now the redirect is working. And it is working on ALL my sites. There is absolutely 100% no reason it should stop working, then all of a sudden *after* I post my question here it suddenly starts working.... no sense. Perhaps I'll go out on a limb and say it's browser caching, because I have a 30-day mod_expires directive in my htaccess. However, I did keep my browser's cache cleared between tests.... I wonder if it's possible that Apache, itself, was sending out cached results? Or my IP caches pages? It's not my area of expertise, so I'm just guessing.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.