Forum Moderators: phranque
When the site is accessed using the www.correctdomain.com and www.wrongdomain.com, everything seems to work and the correct URL is displayed in the browser address bar. However, an "Internal Server" error shows up when trying to access any other pages than the home page (ie: wrongdomain.com/items). All pages--except the secure ecommerce areas--are in the same root directory with the .htaccess file.
Here is the code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.correctdomain\.com
RewriteCond %{HTTP_HOST}!^123\.456\.78\.910
RewriteRule ^(.*) $ [correctdomain.com...] [R=permanent,L]
I have limited coding experience and would appreciate any input or advice. Since this site is busy 24/7, I have not been able to thououghly test this code...I just want to do all my homework before possibly loosing sales b/c the site is down.
Thanks for this great forum! I keep trying to post, but my questions are answered before I can ask them.
teleturner
Welcome to WebmasterWorld [webmasterworld.com]!
You have an extra space in the RewriteRule, preceding the first "$".
mod_rewrite is unforgiving of any typo. It should read:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.correctdomain\.com
# You site's IP address goes in the next line
RewriteCond %{HTTP_HOST} !^123\.456\.78\.910
RewriteRule ^(.*)$ http://www.correctdomain.com/$1 [R=permanent,L]
You could put this .htaccess code into its own subdirectory with a few simple test pages for safer testing, and then promote it to your site's web root when you are sure it's not badly broken. Or you could modify it so that it is only invoked when a test file is requested, and then remove that modification after testing, e.g. - change the rewriterule to only act on one subdirectory path of your site:
RewriteRule ^testdirectory/(.*)$ http://www.correctdomain.com/$1 [R=permanent,L]
For more info and resources, see this Introduction to mod_rewrite [webmasterworld.com].
HTH,
Jim
However, even with the corrected code, I am getting 404 errors whenever www.wrongdomain.com/newdirectory is accessed. Pages accessed by www.correctdomain.com/newdirectory work just fine.
Could this be a DNS issue with www.wrongdomain.com pointing to the same server as www.correctdomain.com? Perhapse I should just park the www.wrongdomain.com? We do have some other URL's (ie: www.wrongdomane.com)that are parked and they seem to be cooperating just fine with the 301 redirect.
Like everyone else, I'm trying to preserve high PR with Google, etc.
Thanks again for the invaluable advice.
teleturner
It is fully expected that www.wrongdomain.com be pointed at the same server, so that should not be a problem.
For one of my sites, I have registered both the .com and .org TLD, even though technically the site is a .org. The .com domain resolves to the same server as the .org. The primary purpose I use this code for is to redirect requests for www.mydomain.com, mydomain.org, and mydomain.com - none of which are "correct" - to www.mydomain.org which is our "branded" Web address.
I looked at the code again several times. If you are using it as posted, having changed only the two domain-name entries, I can't see a reason for your 404's. You will only get a 404 if the RewriteCond or RewriteRule pattern match fails - so investigate that angle first.
HTH,
Jim
Thanks for the help.
I have eliminated the 404 errors when testing in a subdirectory using the following code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.correctdomain\.com
RewriteCond %{HTTP_HOST}!^123\.456\.78\.910
RewriteRule ^directory/(.*)$ [correctdomain.com...] [R=permanent,L]
...but I have a couple of questions before going live.
This site only has 3 subdirectories with 1-20 files in each. If I place the following code in my root directory .htaccess, what changes do I need to make to the RewriteRule so that the permanent redirect will cover all the files (ie: www.wrongdomain.com/directory2/file.html) in all the directories?
I am still a bit confused about the first part of the code: RewriteRule ^(.*)$ I think this was the cause of my 404's when I did a quick test in the root dir. What should be inserted between the ^ and ( when used in the root directory? This is the only thing that I can't quite figure out using the examples given on this forum and the Apache URL rewrite guide.
A quick test in the root using RewriteRule ^(.*)$ produced a 404 on every page (www.wrongdomain.com/file or www.wrongdomain.com/directory2/file.html), but the home page (www.wrongdomain.com) worked fine--although the url did not change to www.correctdomain.com in the browser address bar.
My main priority is to put a 301 redirect on any indexed pages to www.wrongdomain.com. This URL was launched against our request and submitted by an SEO while we were purchasing the company. We're starting to see a merging of the 2 duplicate content urls into wrongdomain.com because it is first alphanumerically.
The help on this forum from members like you has been invaluable. I only hope that I will be able to contribute in the same way some day.
teleturner
^(.*)$ means "any filepath/filename in this directory or below, and remember it for later use as $1"
^ is a start anchor saying roughly "begin required-match here." .* means "any characters," $ is an end anchor, and the parentheses save the characters in the requested URI which match - in this case, the entire path to the requested resource - for use as the variable $1 in the new URL to its right in the rewriterule.
As such, the rule I posted will only work for resources at or below the directory in which the .htaccess containing it is posted. If the requested page or file is not in that directory or subdirectory, you'll get a 404. I believe this explains your problems.
Tip for live testing: Do not delete your old .htaccess on the server, or allow the new one to overwrite it. Instead, upload the new .htaccess with a unique filename, like "new.htaccess". Then, copy the existing .htaccess to a "backup" name, like "old.htaccess". When you are ready to test - oh, about 3:00 AM when traffic is low - rename new.htaccess to .htaccess, and test your home page and a few others. If all is well, continue testing. If not, simply rename old.htaccess to .htaccess to quickly revert to your old working copy. This procedure avoids the delay and risk of having a failed upload while trying to revert to a working version.
If you get errors, capture your server error log. If your error reporting level is set to "error" or below, it will often contain enough information to diagnose the problem.
I encourage you to continue experimenting using mod_rewrite in a subdirectory until you get comfortable with how it works and with how regular expressions pattern-matching works. After a short while, your response to an error changes from an "Oh my (insert deity)" panic to a "Doh!" slight self-annoyance and the utility of it then completely overcomes the initial stress factor. :) For me, mod_rewrite it is a deciding factor in the usefulness of Apache Server - I could not get on without it.
Re: "Thanks for the help, hope I can contribute" - You're most welcome. It is easy to "contribute to WebmasterWorld" - See the link in the very top right corner of any page. Not quite your "contribute in the same way," but needed and appreciated just the same. (Tell 'em I sent you) :)
HTH,
Jim (Who reads much more than he posts)
(To convince SE's to list newdomain.com, and drop olddomain.com without incurring a mirror penalty problem)
Got a client that's adamant about changing "branding" URL, and I don't speak "techese", I just need to know what to tell the techs.
That is exactly what we are doing. Same IP, different domain. Switching to our "Brand Name" domain is important because it reflects the level of service our non-web customers are accustomed to. Our olddomain.com--basically a good keyword--had nothing to do with our company or our image, but an SEO thought it would place well with the SE's.
The above code worked great for most of our site, but we had an issue with the secure e-commerce directory, so we opted not to use mod_rewrite and went with simple 301 redirects in each directory. Works fine so far, we'll se how the next crawl goes.
I would definitely use the mod_rewrite code in this thread to accomplish what your client wants. We just had a secure directory issue we couldn't resolve.
teleturner