URL questions

I have some questions about URLs…

1.) Is it correct that if someone navigates to the following URL’s that there will be 3 different cookies?

mysite.com
www.mysite.com
http://www.mysite.com

2.) Isn’t this type of URL the most “proper”?

http://www.mysite.com

3.) How can I make Apache redirect from either of these…

mysite.com
www.mysite.com

to this…

http://www.mysite.com

Thanks.

http:// shows protocol and it exists in any URL you open in browser actually
Modern browsers just hide it in address bar, but if you copy and paste URL from browser’s address bar to notepad you’ll see protocol is still there.

Here you can learn how to force redirect to WWW subdomain.

If you set a domain level cookie then it will be accessible to all sub domains within mysite.com so the presence or not of the www would then not make any difference.

Is www a sub domain?

Sure, you can have different sites on www.example.com and example.com if you want to.

Here is what I have now…

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

Is that okay?

Why is it that in FireFox that code only yields “www.mysite.com” without the “http://”

Yes, if it works the way you want it to work.

As I’ve said above some browsers can hide protocol in address bar. It is still there though.

Ideally it wouldn’t have a trailing slash for the home page.

I would prefer “http://mysite.com

BTW, the link you provided mentioned that redirects are a hard way of accomplishing this, and then it went on to mention…

What is that all about?

m_w,

What have you got against trailing slashes (on the domain or subdirectory)? They only make life easier for Apache.

Actually, the code given specified the trailing slash on the domain. You MIGHT be able to avoid that by using Apache’s {REQUEST_URI} variable as it actually contains the slash between the domain and URI (in Apache 2.x, it’s hidden by default but used in redirections).

RewriteCond %{HTTP_HOST} !^www\.[color=red](.*)$[/color] [NC] RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

In fact, you don’t need the red text above!

The link showed the preferred way to make SIMPLE redirects using mod_alias. mod_alias is much faster because it doesn’t load the regex engine but it’s not able to pattern match nor access other server variables the way mod_rewrite does. For this redirection (forcing the www. in the domain), mod_alias is the hammer while mod_rewrite is the sledge hammer, i.e., not the proper tool.

If you have other (more complex) redirections, it probably doesn’t matter because you’ll have to load the regex engine anyway to access the power of mod_rewrite.

More information (and examples) about mod_rewrite at my tutorial at http://dk.co.nz/seo.

Regards,

DK

You lost me.

I just figured it looks prettier without a trailing slash, plus I think that is what most people are used to seeing. (To me, a trailing slash means “directory”.)

I don’t understand what you are recommending.

Above I asked if this was overkill…

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

I also asked about what this was and how to implement it…


Using the rewrite engine is a pretty heavyweight way to solve this problem. Here is a simpler solution:

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / http://www.example.com/
</VirtualHost>



And then youll have another < VirtualHost > section with ServerName www.example.com for your real server configuration. Apache automatically preserves anything after the / when using the Redirect directive, which is a common misconception about why this method wont work (when in fact it does).

apache redirect from non www to www

m_w,

DocumentRoot IS a directory, however, the code you used REQUIRED the / for a null {REQUEST_URI} so the “error” was with your redirection statement.

What I was recommending was to learn the first rule of mod_rewrite: Only use mod_rewrite when there is not a better/faster tool, in this case, mod_alias.

Your code was not really overkill but was the cause of the trailing slash on the domain only redirection. The same applies to your virtual host code (eliminate the trailing / if you don’t like to see it) from StackOverflow.

If you want example code for real world problems which are easily resolved using mod_rewrite, try http://dk.co.nz/seo rather than post links to StackOverflow which are creating the problem you’re trying to solve.

Please note that one of my pet peeves is for a “webmaster” to use code when he has no clue as to what it does - and that fits this case precisely (with regard to your use of StackOverflow code). If you have questions about my mod_rewrite tutorial or any of the code therein, you know where to find me - although the explanations that are associated with my code samples should make it very clear what they do…

Regards,

DK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.