Forum Moderators: phranque
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?newdomain\.com\.?(:[0-9]+)?$
RewriteCond $1 !^folder/
RewriteRule ^(.*)$ /folder/$1 [L]
This works great. it keeps the newdomain.com in the url bar which is very important. When I insert something like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?newdomain\.com\.?(:[0-9]+)?$
RewriteCond $1 !^folder/
RewriteRule ^(.*)$ /folder/$1 [R=301,L]
The redirect still works, but it shows the olddomain.com/folder in the address bar as opposed to newdomain.com. Why would making it permanent change the URL? Is there a better way to do this so search engines can track this? Any help is most appreciated.
However, I'm not sure if you are using the right method here. A redirect makes the browser request a different URL with a new request. A rewrite connects a URL request to an internal filepath - without revealing what that filepath actually is.
.
Your first example is for a rewrite.
Your second example is for a redirect, and if the domain name is being 'changed' then it is likely you have used CanonicalName on, and the default server domain name is the old domain name.
Two things: You should always specify the correct domain name in the target URL of a redirect (That is, if the rule has R=301 in it, then it should also have the domain name in it), and you might want to look at your usage of CanonicalName On and/or change the default server domain name to be the new domain name for the site..
Properly implemented, that rule is totally invisible to search engines, which will not know (or care) that your content is being served from the /folder subdirectory. That fact is completely irrelevant and unimportant to search engines anyway; they care about URLs on the Web, not file structures inside servers.
Look elsewhere for the problem.
Jim
On the other hand, a redirect tells a browser when it asks for URL A that it needs to make a new request, and in that new request that it needs to ask for URL B.
The robots.txt can be very easily tested. It affects the domain that it was accessed from as seen in the browser address bar. Upload the file for thisdomain.com and check you can read it when you ask for thisdomain.com/robots.txt and then ask for thatdomain.com/robots.txt - if you can see the same file contents then it will also apply to URLs as accessed via thatdomain.com.
So you've got rather a mess here, and the solution is to add a different kind of redirect. In order to prevent further disasters, I'll include the original rewrite as well, and both of these rules must be used.
RewriteEngine on
#
# If the client directly requests a URL-path starting with /folder/ from either olddomain or newdomain
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /folder/
# externally redirect the request to newdomain.com, and remove /folder/ from the URL-path
RewriteRule ^folder/(.*)$ http://newdomain.com/$1 [R=301,L]
#
# Internally rewrite requests for newdomain.com/<path> and www.newdomain/<path> URLs
# to /folder/<path> files:
# If requested hostname is www.newdomain.com or newdomain.com, with optional FQDN or port number
RewriteCond %{HTTP_HOST} ^(www\.)?newdomain\.com\.?(:[0-9]+)?$
# and we haven't already rewritten this request to /folder
RewriteCond $1 !^folder/
# Then internally rewrite this request to add "folder" to the filepath
RewriteRule ^(.*)$ /folder/$1 [L]
The complex hostname pattern and description in the second rule points out another shortcoming: You should add one or more rules after the first one shown here in this post to canonicalize all requested hostnames to either www- or non-www domains. You can choose either, but you should choose -- and you should be utterly consistent in linking to the canonical domains only from your own sites.
Jim