Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize apache example.com => www.example.com #11

Closed
missjojo opened this issue Dec 13, 2013 · 6 comments
Closed

optimize apache example.com => www.example.com #11

missjojo opened this issue Dec 13, 2013 · 6 comments

Comments

@missjojo
Copy link

use

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_ADDR} !=127.0.0.1

replace

RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} !=localhost [NC]
RewriteCond %{HTTP_HOST} !=127.0.0.1

because:

  1. save bits;
  2. cover some non localhost virtual host such as a DIY xx.dev instead of localhost using hosts.

anyone agree?

@alrra alrra closed this as completed in fb1699d Dec 17, 2013
@alrra
Copy link
Member

alrra commented Dec 17, 2013

Thanks @a25ce1!

Change made in fb1699d, but left localhost in (see: h5bp/server-configs#152).

@missjojo
Copy link
Author

Thank you. But my other suggest is change RewriteCond %{HTTP_HOST} !=127.0.0.1 to RewriteCond %{SERVER_ADDR} !=127.0.0.1. Not just remove RewriteCond %{HTTP_HOST} !=localhost [NC].

This is good for automatic adjust to all virtual name for local environment, not just only for 127.0.0.1 and localhost, but also for other-local-virtual-names. 😄

@alrra
Copy link
Member

alrra commented Dec 18, 2013

RewriteCond %{SERVER_ADDR} !=127.0.0.1

@a25ce1: Sorry I've kinda missed that, mainly because the first time I've tested it didn't work on my machine (the reason was localhost was mapped to ::1, so %{SERVER_ADDR} ended up being ::1 instead of 127.0.0.1).

Thanks for bring it up again!

From http://en.wikipedia.org/wiki/Localhost:

The resolution of the name 'localhost' to the most-commonly used loopback addresses is set up by the following lines in each device's hosts file:

127.0.0.1 localhost
::1 localhost

So, I think a better solution would be:

<IfModule mod_rewrite.c>
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{SERVER_ADDR} !=127.0.0.1
    RewriteCond %{SERVER_ADDR} !=::1
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>              

@alrra alrra reopened this Dec 18, 2013
@alrra alrra closed this as completed in 994cd1f Dec 18, 2013
@missjojo
Copy link
Author

That's right. I get new knowledge from you. You are very kind. Thanks! 😉

@jaimergp
Copy link

I know this is is old, but... who would want a non-www to www redirect when accessing via server IP? I know it's a corner case, but I think there's no harm in adding another condition like:

RewriteCond %{HTTP_HOST} !([0-9]{1,3}\.){3}[0-9]{1,3}

Correct me if I'm wrong please!

@mathiasbynens
Copy link
Member

@jaimergp I expect everyone to be redirected to the one, true, canonical version of the URL they accessed. You wouldn’t want the site at the server IP to be indexed separately by search engines, would you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants