• Resolved archard

    (@archard)


    Recently I became interested in making it so that whenever someone visited my site without a www in it, the server would automatically fill it in for them. For example, if someone typed in gametabs.net, they would be redirected to https://www.gametabs.net instead. This is so search engines don’t get confused and list you twice.

    I looked into a lot of htaccess stuff and that works for the most part except for one problem. If someone types in ‘gametabs.net/otherpage’ they get redirected back to the homepage https://www.gametabs.net, when I want them to be directed to https://www.gametabs.net/otherpage. I think this has something to do with wordpress, so I did some more searching and found a plugin (which can be found here) for wordpress that does basically the same thing but doesn’t mess with htaccess, and still no luck.

    Is there a way I can make it so that no matter which page a user goes to, it just makes sure that theres a www in front of the URL?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter archard

    (@archard)

    Bump. Someone’s gotta have some insight.

    I don’t know about your browser(s), but mine – FF 1.5.0.7 and IE7 both do what you want automatically.
    Hence I type in gametabs.net/otherpage I arrive at https://gametabs.net/otherpage which of course is a 404.
    Works the same for working pages.
    Don’t know why but your main page is down.

    https://gametabs.net should resolve automagically to https://www.gametabs.net if you have things setup correctly with your host.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    I do this, but in reverse. I redirect www to the no-www domain.

    Still, put this at the top of your .htaccess, it will solve your issues.

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

    Obviously, change the example to your domain name.

    Thread Starter archard

    (@archard)

    Otto — I tried this and many other htaccess stuff before, and what happens is if I type in https://gametabs.net/otherpage, the user will get redirected to the homepage.

    For example https://gametabs.net/nes gets redirected to https://www.gametabs.net/index.php when I want it to redirect to https://www.gametabs.net/nes

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    The specific example I gave will *not* redirect to the homepage. It will redirect to the same page, but with the www.

    Notice how this URL (on my blog) redirects from a www to a non-www, without going to the homepage:
    https://www.ottodestruct.com/blog/2007/01/26/enable-firefox-spell-checking-in-wordpress-21/

    This does need to be in the .htaccess of your site root, whatever that is. So if you have the blog in a subdirectory, then you’re not going to add it to that directory, you’re going to add it to the main site’s directory.

    The key lies in this:
    RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

    The two important bits here are the (.*) and the $1. The (.*) matches whatever the rest of the URL is. The $1 fills in that URL for the redirection.

    “I do this, but in reverse. I redirect www to the no-www domain.”

    I always recommend the same thing too, primarily because “www” is very ugly and pretty useless. After all, mail servers do not require you to send emails to “[email protected]”, do they?

    These folks privide an interesting insight into why the use of “www” is pretty silly. And of course, the folks behind no-www.org who provide a compelling enough argument why the use of “www” is depreciated anyway.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    I always recommend the same thing too, primarily because “www” is very ugly and pretty useless. After all, mail servers do not require you to send emails to “[email protected]”, do they?

    To be fair, mailservers have a specific DNS record specifically for that reason. Without a properly setup MX record, you would indeed need to send your mail to [email protected]. Web servers have no such DNS entry. The original reason for the www prefix becoming commonplace was that when the web was new, the webserver machines were usually different boxes with different IP’s. The existing domain name’s IP was already in use by other boxes, for other reasons. So the subdomain was needed to direct web traffic to the right machine.

    When the web became more commonplace, and basically became the main focus of having a domain name in the first place, then it is really no longer necessary. Along with advancements in routing and such, you can have much more complex setups and it makes more sense to distinguish differing traffic by port instead of forcing a subdomain name. So www is indeed deprecated and unnecessary. It’s pretty much assumed that a domain name has a web site at that domain name, if they have a web site at all. So there’s no point in having www.

    That said, a lot of people still type it, so I use 301 redirects to force them to the non-www version, of course. Pretty much exactly as no-www.org recommends.

    On the other hand, there’s specifications out there to change the way DNS works to allow this sort of record to be specified more generically. So you could have something like the MX record for any given protocol you like. Jabber uses something like this, as does iTunes and Apple’s various protocols (in an admittedly different way, but with the same basic formats).

    Thread Starter archard

    (@archard)

    Otto — that’s pretty interesting stuff. I never really thought why we use www in the first place, I always just thought it looked better. But after some research I decided I’m gonna go without the www.

    However,

    The code you gave me still gives me the same problem. This time I set it up to redirect to https://gametabs.net – no www. If I type in https://www.gametabs.net it redirects me to gametabs.net. No problem. But if I go to anything other than the home directory AND I use the www, I get redirected to gametabs.net/index.php. I want it so that, for example, if I go to https://www.gametabs.net/nes, it will redirect me to gametabs.net/nes.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    archard: Are you certain? This should work for you. It works for me.

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.gametabs.net$ [NC]
    RewriteRule ^(.*)$ https://gametabs.net/$1 [R=301,L]

    Note that this *must* be at the top of your .htaccess file, before any of the WordPress stuff. Also make sure that the Blog and WordPress URI’s on the Options page reflect the correct address that you want the site to use.

    Thread Starter archard

    (@archard)

    Well that was stupid of me, didn’t do either of those. Thanks man I appreciate the help.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Forcing a www in the URL’ is closed to new replies.