What's the code to have site.com redirect to www.site.com ? I want to use safe 301 redirect using modrewrite. Please help me and give me the code. Thanks!<3
Place the following in a .htaccess file in the root of your domain (domain.com/.htaccess): Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^domain\.com$ RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L] Code (markup):
i strongly suggest you to add the code which is given rodney88 on reply two. you must change the url according to your domain. this code will help you to avoid supp. results from site
Why is that slash there? domain[b]\[/b].com Code (markup): Why's that there? This is one of my .htaccess for one of my sites, and it gets high rankings in Google: Options +Indexes Options +FollowSymlinks RewriteEngine on rewritecond %{http_host} ^domain.com [nc] rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc] Code (markup): Can you explain to me what the difference between my code and your code is? As you can see in mine, I have [nc] in brackets (I don't know what it is or what it does) and I have no \ in my domain before the dot (.). Please assist.
Here is mine, I use it on all my sites, I also have a custom 404 page. RewriteEngine on RewriteCond %{HTTP_HOST} ^******\.info RewriteRule ^(.*)$ http://www.******.info/$1 [R=permanent,L] ErrorDocument 404 /404.php
The stuff in the brackets are called "flags", [nc] means "no case" which means it's not case sensitive (it will see HeLlO the same as hello). The extra \ isn't needed, but it's used as an escape character.. reason is . is a REGEXP function (okay wrong wording bear with me), so to prevent it from being parsed as so the escape character is added so it's just interpreted as a period. Probably a bad explanation, but hopefully it answered your question
Flags modify the behaviour of the rule - R forces a redirect (by default a 302 redirect but we can specify 301 with the =301. NC makes it case-insensitive - but the hostname is always lowercase so it's unnecessary here. L is for "Last" and forces the rewrite module to stop looking for or applying any more rewrite rules - once you've sent out your 301 moved header, you don't want to do any more rewriting. Just to expand a bit on CodyRo's explanation - in a regex expression the dot is a metacharacter (special character). It will match any character at all, and is used in that sense in the next line for the RewriteRule itself. But when we're checking the hostname, we want a literal period and not the wildcard metacharacter (which would match against any character). To get a literal period we simply escape it's special properties with the backslash. Obviously it still works without being escaped since a period falls into the category of "any character".
Thanks for the explanation guys. Rep added. Just for further clarification...nc is not needed? I guess not huh, since it's just being used for the redirect?
Does the change take effect immediately? I've just pasted the code to my .htaccess, but I don't notice any change. I have changed the 'domain' part to my domain, but when I access http://mydomain.com it isn't redirected to http://www.mydomain.com. I'm wondering whether this is normal.
Replace the word your domain.com WITH your ORIGINAL DOMAIN AND TLD Yes, the change takes place immedaitely
Yes, my domain is a .com. I tried it again just now and it works. I don't precisely know why it didn't work yesterday, since what I did just now is the same as what I did yesterday (and I've saved the file after modifying via the file manager in cpanel). But weirdly, when I opened the file today, all the modifications I pasted there yesterday was gone, so I did the same thing again and... it works. Thanks for the help!
Maybe you just forgot to save it after you modified it. I do that a lot, and then get all angry with any errors thinking I did not make an error, because I am perfect.
The change takes place immediately to new visitors to your site, but your ISP/computer may cache your site and you don't see the results unless the cache has been cleared.