Forum Moderators: phranque
In my public_html .htaccess file I've got this code to redirect www to non-www
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
</IfModule>
It works fine for that purpose. However I have a subdomain that it's effecting in a different way. It redirects sub.example.com to example.com/sub which I don't want it to do.
Humans or spiders don't visit this subdomain so I don't care about the www or non-www part of it on the sub. Is there any way to change this code so it doesn't redirect my sub to example.com/subfolder?
Thanks!
[edited by: jdMorgan at 2:04 am (utc) on July 25, 2007]
[edit reason] example.com [/edit]
www.example.com is just a special case of subdomain.example.com, so you should in/exclude special case(s) in a RewriteCond.
OK I understand that in theory but what exactly should the code say so that it works for www but not another subdomain?
Sorry, I just cut and paste these things. I don't know how to write them ;)
Start with the Apache mod_rewrite documentation [httpd.apache.org], especially that for the RewriteCond [httpd.apache.org] directive. See the list of server variables that RewriteCond can examine. Look at the code you already have, and determine what you do and do not understand about it. Look up the parts you don't understand, and feel free to ask here if you cannot find information about a specific question.
The next time you are faced with a problem that mod_rewrite can be used solve, you will have experience, familiarity, and understanding gained from this simple exercise. You will be able to solve your problem quickly, and perhaps, if we're lucky, you will be able to contribute some of the time you saved here, helping others to find solutions to their problems... :)
I've posted this many times, but it is worth repeating: Mod_rewrite code is server configuration code. Every single character in mod_rewrite code must be correct, both syntactically and functionally (in the context of your server hardware and software environment), or it can have devastating effects on the operation of your server, on your search engine rankings, or both. To use mod_rewrite code --even the simplest-- without understanding exactly how it works and exactly what it does on your server, is potentially very dangerous.
You can fix your problem by adding a single line to your code, which will be almost identical to the RewriteCond that you already have, differing only in the value for the hostname pattern.
Jim
As documented in our forum charter, the purpose of this forum is to help you write your own code, and not to write it for you. If someone here writes it for you, then you will learn almost nothing, and will likely have to come back here for more code later. This is not a sustainable "model" for this forum, as there are many askers and few answerers here.
I get what you're saying and I'm not trying to bust up the model of the forum. We all have our specialties and servers are not one of mine. I do what I have to do or what I can do quickly, but it doesn't make economic sense for anyone to try to learn every detail about every single facet of how the internet works.
If it's something beyond what I know how to do or can conceivably learn to do quickly then I ask, search, or hire it out. All I was doing was asking.
Since I know this stuff is extremely precise and can screw things up if it's done incorrectly (as you said also.) And since I obviously have the basic code, and figured it was a simple fix (as you said: "You can fix your problem by adding a single line to your code") I didn't figure I was being a forum leech just for asking.
It's like if I had a whole paragraph in Spanish but didn't know one word and so asked someone how to write it. Sure they could say "we're not here to teach you Spanish. Go away and learn Spanish and then if you still don't understand come back and ask us what the word is." It doesn't make sense does it? Wouldn't it be easy to just help someone out and tell them the word?
You'll notice that I'm not just some freeloader who just signed up today to get free information. I've given plenty of advice and help over the years just not on this particular board. I didn't realize there was a "per-board-helpful-score-index" being kept.
You can fix your problem by adding a single line to your code, which will be almost identical to the RewriteCond that you already have, differing only in the value for the hostname pattern.
You may as well have finished your thought..."but I'm not telling you what it is nya nya nya."
But seriously Jim, I get your point in general. I think you've gone a bit overboard with it. I'm not mad about it, just surprised. One of the best things about WebmasterWorld is the helpful people. I hope they're still around.
Jim, I hope my rant in response to your rant has not caused any hard feelings. :) I'll wait for this post to be deleted. ;)
RewriteCond %{HTTP_HOST}!^specificsubdomain\.example\.com [NC]
RewriteCond %{HTTP_HOST}!^example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
however this might not be what you really want because it would also rewrite www.specificsubdomain.example.com as well as anyothersubsubdomain.specificsubdomain.example.com to example.com.