Home > database >  RewriteCond and RewriteRule work fine except when not using paths
RewriteCond and RewriteRule work fine except when not using paths

Time:10-24

I have a new domain, that needs to load the contents of an old domain via Proxy in Apache. This is already working fine:

<VirtualHost myserver:80>
ServerName newdomain.com
ServerAlias www.newdomain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^newdomain\.com$
RewriteRule ^(.*)$ https://olddomain.com/$1 [P]
</VirtualHost>

For example:

  • newdomain.com/contact loads olddomain.com/contact
  • newdomain.com/catalogue/342 loads olddomain.com/catalogue/342

But the problem comes when I try this:

  • newdomain.com (without any additional paths). The result is a 404 error.

I pretty much guess that the issue is in the RewriteCond / RewriteRule definitions, but I can't seem to find the problem. Any ideas? Thanks!

CodePudding user response:

RewriteRule ^(.*)$ https://olddomain.com/$1 [P]

Because this rule is defined in a virtualhost context, the URL-path that the RewriteRule pattern matches against is root-relative and starts with a slash. Since this is captured and used in the substitution string it will result in a double slash at the start of the target URL-path.

Maybe this is throwing off the routing for the homepage.

Try the following instead:

RewriteRule ^/(.*)$ https://olddomain.com/$1 [P]

There's also no need for the preceding RewriteCond directive since you are already in the vHost for newdomain.com and the current condition omits www.newdomain.com, which is presumably not the intention.


Or don't use mod_rewrite at all, since it would seem to be a direct one-to-one proxy.

For example, use the following instead.

ProxyPass / https://olddomain.com/
ProxyPassReverse / https://olddomain.com/
  • Related