.htaccesshttp-redirectmod-rewriteapache2

Apache2 rewrite rule match HOST and URI to redirect another host with the same path


I have CNAMES and I have them direct to different directories. This part works. However, I would like to be able redirect

and

and include paths after dl or todigra in the redirect.

With access dl via todigra.org and vice versa I get some problematic indexing with scholar.google.com etc.

As said, the first two rewrites does what they should do but I can't figure out how to formulate the second ones:

RewriteCond %{HTTP_HOST} ^dl\.digra\.org
RewriteRule ^$ /index.php/dl [R=permanent,L]

RewriteCond %{HTTP_HOST} ^todigra\.org
RewriteRule ^$ /index.php/todigra [R=permanent,L]

RewriteCond %{HTTP_HOST} ^todigra\.org
RewriteRule "^/index.php/dl/(.+)$" "http://dl.digra.org/index.php/dl/$1" [R=permanent,L]

RewriteCond %{HTTP_HOST} ^dl\.digra\.org
RewriteRule "^/index.php/todigra/(.+)$" "http://todigra.org/index.php/todigra/$1" [R=permanent,L]

The two last rewrite rule condition pairs do not seem to do anything. I was expecting this to rewrite: https://todigra.org/index.php/dl/article/view/1898 as https://dl.digra.org/index.php/dl/article/view/1898.


Solution

  • RewriteCond %{HTTP_HOST} ^todigra\.org
    RewriteRule "^/index.php/dl/(.+)$" "http://dl.digra.org/index.php/dl/$1" [R=permanent,L]
    

    The URL-path that the RewriteRule pattern (1st argument) matches against does NOT start with a slash, so these two rules will never match. However, neither would they match /index.php/dl as stated in your opening paragraph (or even /inded.php/dl/) since your regex is only matching /index.php/dl/<something>.

    You would need to do something like the following instead:

    RewriteCond %{HTTP_HOST} ^todigra\.org [NC]
    RewriteRule ^index\.php/dl(/.*)?$ http://dl.digra.org/index.php/dl$1 [R=permanent,L]
    

    This now matches index.php/dl and index.php/dl/ and index.php/dl/<something> and redirects to the corresponding URL.

    Note that I included the slash in the backreference, so this is removed from the substitution string.

    Note that you are redirecting to HTTP, not HTTPS? But you mention HTTPS in the URL(s) in the last paragraph?


    Aside:

    However, implementing an external redirect in this scenario looks wrong to begin with. It looks like you are redirecting from the desired canonical URL to the non-canonical/hidden URL? index.php should not normally be in the visible URL if you can help it. It looks like these should be internal rewrites (as you state in your last paragraph, but that might just be a mix of the terminology? rewrite/redirect?) coupled with two canonical redirects to correct the hostname and remove the non-canonical URL-path?

    And the first two redirects that you say "work" only redirect requests for the root (no URL-path), whereas the second two redirects handle a URL-path. This does not look correct. What if you get a request for https://dl.digra.org/<something>, for instance?