Forum Moderators: phranque
Question is, how do I do a silent (internal) redirect, and should I (or when should I)?
Reason is this: Website is accessible as either www.domain.com or domain.com, but the secure certificate is only for www.domain.com. I would like to redirect all the domain.com requests to www.domain.com. Should I use 301, 302, or internal? I do not have access to http.conf.
Thanks
-brandi
In order for your certificate to work, you'll need to use a 301 or 302 external redirect. Unless you plan to get a certificate for the www-less domain soon, I'd use a 301. For use in .htaccess:
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=permanent,L]
Jim
That's pretty much what I thought, just wanted to make sure.
I'm also renaming some files to more user friendly names (dsc_rps.htm to product-name.htm). It seems like a 301 redirect would be best here for spiders. Would you mind telling me when one would use a silent (internal) redirect?
Thanks again
-brandi
When using the above rewrite, now when I go to
[mysite.com...]
I get redirected to [mysite.com...]
which is half correct, BUT blah.htm should return a 404 (blah.htm does not exist).
Now I'm stuck again.
For renaming pages, a 301 is the way to go. You can remove these redirects once most of the incoming external links to them have been corrected, the search engines have stopped accessing the old URLs, and once most of your users seem to have updated their bookmarks.
Internal redirects are used mostly for site security issues, such as redirecting image hot-linkers to an alternate image or for invoking a bad-bot trap from an innocent-looking URL, and for making scripts spider-friendly by rewriting spider-friendly URLs to those required to pass parameters to your scripts.
Jim
I'm not sure, since you did not say what you did get instead of a 404. But I'll take a guess.
If you are using the ErrorDocument directive, be sure that you are not specifying a canonical URL.
ErrorDocument 404 [yourdomain.com...] is incorrect. Use:
ErrorDocument 404 /yourerrorpage.html
Try the server header checker [webmasterworld.com] to investigate what your server returns from each domain for non-existent pages. For the non-www domain, you should get a 301 response accompanied by the URL including the www domain. For the www domain, you should see a 404 response.
Jim
second question: what does the 1 following the second $ do in this rule?
RewriteRule ^(.*)$ [mydomain.com...] [R=permanent,L]
Thanks again!
-brandi
I was using the server header checker, which is where I saw what was happening.
For the www.mydomain.com/blah.com non-existent page I get a 404 response (good).
For the mydomain.com I get a 301 response (good).
For the mydomain.com/blah.htm non-existent page I get a 301 response (is this correct even though blah.htm does not exist?).
mod_rewrite operates in the Apache API phase where URLs are translated to filenames. Therefore, the 301 is invoked before Apache has checked to see that the file does not exist. When the user-agent follows the 301 and re-requests "blah.hrml" from the "correct" domain, a 404 is returned.
So you are seeing the expected behaviour.
Jim
...and as birdman points out, $1 is used to backreference the page name in the pattern on the left side of the RewriteRule. Without the backreference, all requests for all resources on yourdomain.com would be redirected to the default page of www.yourdomain.com. So, for example, a request to [yourdomain.com...] would be redirected to [yourdomain.com...] which would normally be [yourdomain.com...] - Not a pretty sight if unintended!
Jim