Hallo,
I’ve got two rewrites that force a “www” in the url, one that I found looking around and one that the sysadmin found on another site of ours. I’ve like to know if there are disadvantages of one over the other… one does haz the lazy regex, but the other one is little better.
The site currently has some sort of redirect (possibly done with PHP) to remove index.html/index.php but I don’t know why really, all the other .php file extensions were left in.
So http://sitename.eu needed to always go to http://www.sitename.eu and there may not necessarily be a request uri after it.
I believe mine came from askapache, but it looks identical to DK’s:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\\.sitename\\.eu$ [NC]
RewriteRule .? http://www.sitename.eu%{REQUEST_URI} [R=301,L]
.? would be possibly “a” character, but any type. Any junk characters (don’t match any real file names) cause a redirect to the main page (again I believe using PHP not Apache). I’m possibly confusing my regexes because I’m thinking ? referred to only one char. houses? just the last s is optional.
The RewriteRule says to match zero or one of anything then redirect to http://www.example.com with the original {REQUEST_URI}.
What if there’s more than one of something to match?
What he found and has been working on another site of ours:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Here the (.*) could be a good thing since we don’t want to insist on anything after the HOST… nothing should match nothing and that should be ok, right? If the match contains a leading / then that would get appended to $1 but this doesn’t seem to cause an issue: www.sitename.eu//////filename.php just goes to filename.php (possibly PHP is doing another redirect?), unless filename.php doesn’t exist, so then to main page.
I want to know if someone with more experience can see any obvious disadvantages between the two… am I wrong to have the RewriteCond match a full particular domain name? Or is that better? Or is it 6 of one and a half dozen of another? What to insist on if anything after the domain name is optional (and currently no ?=query strings but I don’t know that it will remain so!).
Again, junk characters and “index.php” or “index.html” both get redirected to the domain name alone.
He’s putting these in .htaccess, and the sites are virtual hosts.