1

I've already looked at many samples and tutorials how to set up those 301 redirects on Apache and can't figure out why some are not working. Whenever I want to redirect any still existing page from the old domain to the new domain the page on the old domain is still used. But it "works" if you type in a page which does not exist on the old domain. The same is true for directories which do not exist on the old domain. So whenever you type a non-existing URL (page or folder) the redirections is taking place (but of course produces 404 errors of on the new domain).

In addition it also works for the domain name itself. So just typing "old.com" correctly redirects to "new.com".

Any input is much appreciated because this is slowly driving me crazy :)

EDIT: I added the complete htaccess file.

EDIT 2: So I removed almost all redirects and currently my htaccess looks like this:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule ^(.*)$ "http://www.new.com/$1" [R=301,L]

The only redirect that is working is the simple one from old.com to new.com. A redirect like old.com/page.htm to new.com or even new.com/page.htm is not working.

EDIT 3: This is the .htaccess of the top-level domain:

# -FrontPage-

IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName www.toplvl.com
AuthUserFile /home2/a_user/public_html/_vti_pvt/service.pwd
AuthGroupFile /home2/a_user/public_html/_vti_pvt/service.grp

RewriteCond %{HTTP_HOST} ^toplvl.com [OR]
RewriteCond %{HTTP_HOST} ^www.toplvl.com
RewriteRule ^(.*)$ http://www.toplvl.com/$1 [L,R=301,NC]
10
  • "only the second one is working" - The comments in your code block seem to suggest that #2, #3 and #4 are working? Just to note, the RewriteRule substitution (2nd argument) is a normal string, not a regular expression, so ordinarily you do not need to escape anything or surround it in double quotes. You've tagged your question cPanel - however, I assume you aren't using cPanel to apply these redirects (cPanel is likely to cause more problems)?
    – MrWhite
    Jun 13, 2014 at 13:28
  • Just to clarify... you have no other .htaccess files in subdirectories? And this .htaccess file is in your document root? (Bizarrely, I seem to remember another question recently that had the same problem... old.com to new.com worked OK, but old.com/page to new.com/page didn't - I can't find it though at the mo.)
    – MrWhite
    Jun 13, 2014 at 13:47
  • For this add-on domain I have no other .htaccess files on other subdirectories. For the "main" domain, which is the paren folder for the add-on domain, I have an .htaccess in the folder of the top domain. Regarding cPanel: originally I added the redirects using the cPanel tool, however this didn't work so I started to edit the file manually.
    – Clemens
    Jun 14, 2014 at 9:54
  • "I have an .htaccess in the folder of the top domain" - the .htaccess file for the main domain is likely being processed before the .htaccess file for the addon domain (which, in this case, is located in a subdirectory). Double check this by setting an environment variable in the .htaccess file and printing this to your page. If this is the case then the .htaccess file for your top domain might be conflicting.
    – MrWhite
    Jun 14, 2014 at 12:05
  • Since this is an addon domain pointing to a subdirectory off the main account you will need to add the contents of the ".htaccess in the folder of the top domain" to your question. Can you also define "not working"? a) Nothing happens b) Error c) Unexpected behaviour?
    – MrWhite
    Jun 15, 2014 at 16:19

1 Answer 1

1

I guess you should not enclose your new.com with quotes "", neither scape it because your regular expression is inside RewriteCond, not inside the target.

Try something like this:

Options +FollowSymlinks
RewriteEngine on


RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule ^some\-page\.php$ http://www.new.com/some-other-page.php [R=301,L]
RewriteRule ^(.*)$ http://www.new.com/$1 [R=301,L]

Edit: I've added specific url redirection.

As you can see, order matters, is you have something specific you should place it upfront other rules that are more generic like host redirection.

If you need to play a little bit with htaccess you should use a tester like this. You have to enter urls like http://www.old.com/somepage.php, or http://www.old.com, always using http://.

6
  • Your example seems to imply that the RewriteCond directives applies to both RewriteRule directives that follow - this is not the case. The RewriteCond only applies to the first RewriteRule. So, the code as it stands will result in a rewrite loop.
    – MrWhite
    Jun 13, 2014 at 13:25
  • Hi @w3d, I've tested it, it does not result in rewrite loop because the L(ast) option.
    – Binarysurf
    Jun 13, 2014 at 15:37
  • The L option (on the last rule) is irrelevant here. Whether this results in a rewrite loop is dependent on how the add-on domain is setup. If the two domains point to the same place, which is what I assumed, as otherwise there is no need to check the %{HTTP_HOST} - then the second RewriteRule will result in a rewrite loop (since old.com and new.com are being served the same .htaccess). If the add-on domain points to an entirely different location then you are right, there is no loop, but the RewriteCond directives are not reqd in this case. We would need the OP to confirm this.
    – MrWhite
    Jun 13, 2014 at 16:12
  • I agree on "L opttion (on the last rule) is irrelevant", of course, there is nothing more, in this example it's the end of htaccess, I'm used to write it like that because maybe I'm lazy and I don't want to review working rules if I have to add more at the end.
    – Binarysurf
    Jun 13, 2014 at 16:27
  • The add-on domain is located in a subfolder of a different "main" domain. And yes, this domain has also a .htaccess file.
    – Clemens
    Jun 13, 2014 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.