0

My domain domain.com is redirected to www.domain.com but, due to internal redirects, it only works in some cases:

  • domain.com/folder/ should redirect to www.domain.com/folder/ but it is redirected to www.domain.com/blog/index.php

The issue here is that internally, www.domain.com/folder/ uses that blog/index.php throught a RewriteRule:

RewriteRule ^$          ../blog/index.php

I don't know why, but you are redirected to the script instead of the nice URL.

IT WORKS here (redirection from domain.com/folder/ to www.domain.com/folder, when all code within same "Directory" entry):

<Directory /var/www/vhosts/domain.com/httpdocs/>
    Options -All +FollowSymLinks
    RewriteEngine on

    # redirect non-www to www
    RewriteCond %{HTTP_HOST}        ^domain\.com
    RewriteRule ^(.*)$          http://www.domain.com/$1 [R=301,L] 

    RewriteRule ^folder/$           ../blog/index.php                       
</Directory>

It does NOT WORK here (redirection from domain.com/folder/ to www.domain.com/blog/index.php, when 2 "Directory" entries are used):

<Directory /var/www/vhosts/domain.com/httpdocs/>
    Options -All +FollowSymLinks
    RewriteEngine on

    # redirect non-www to www
    RewriteCond %{HTTP_HOST}        ^domain\.com
    RewriteRule ^(.*)$          http://www.domain.com/$1 [R=301,L]                      
</Directory>
<Directory /var/www/vhosts/sonidon.com/httpdocs/folder/>
    RewriteEngine on
    RewriteRule ^$          ../blog/index.php
</Directory>

Tested also using Options +FollowSymLinks +SymLinksIfOwnerMatch in both directories with no luck :(

I saw a solution here using VirtualHosts instead of Directory: Generic htaccess redirect www to non-www but think that it should be a way to do this because this trouble is not only related to "non-www to www" but to any redirection.

Community
  • 1
  • 1

1 Answers1

0
<Directory /var/www/vhosts/domain.com/httpdocs/>
    Options -All +FollowSymLinks
    RewriteEngine on

    # redirect non-www to www
    RewriteCond %{HTTP_HOST}        ^domain\.com
    RewriteRule ^(.*)$          http://www.domain.com/$1 [R=301,L]                      
</Directory>
<Directory /var/www/vhosts/sonidon.com/httpdocs/folder/>
    RewriteEngine on
    RewriteRule ^$          ../blog/index.php
    #ADDED LINE BELOW
    RewriteRule ^/$          ../blog/index.php
</Directory>

Try the line I added above. I think the issue is that "http://domain.com" was getting redirected to "http://www.domain.com/" (note the trailing slash). Then you were redirecting in the second group if there was no uri ^$, but in fact the uri was "/".

Tim Hoolihan
  • 12,316
  • 3
  • 41
  • 54
  • Thanks for your help, but that doesn't solve the issue. I have tried that line both above and above the another rewrite but it has no effect. I have also tried with different destination scripts for each of those 2 RedirectRules, and "RedirectRule ^$" is the one executed. The most surprising of this issue is that I have been seeking deeper in StackOverflow and Google, and haven't found anyone with the same trouble. Thanks a lot for your effort! – Ben Ockley Jul 11 '12 at 14:11