Rewrite problems on Apache2

The Ubuntu version is:
Distributor ID: Ubuntu
Description: Ubuntu 20.04.5 LTS
Release: 20.04
Codename: focal

The Apache2:
Server version: Apache/2.4.41 (Ubuntu)
Server built: 2024-07-17T18:58:09

I have following lines in my conf file:

RewriteCond %{REQUEST_URI} !/user/login
RewriteCond %{REQUEST_URI} !/contactus
RewriteRule ^(.*)$ https://newsite/$1 [R=301,L]

I want to achieve the following:
If the sub-string is NOT ‘/user/login’
and it is NOT ‘/contactus’ then redirect.

In other words if there is one of these two sub-strings then do not redirect.

That rule fails though. The rewrite is redirecting all the time, the match is not found.

The first three lines from log file are:
[Sat Feb 08 18:08:37.574572 2025] [rewrite:trace2] [pid 723835] mod_rewrite.c(494): [client 1.1.1.1:52738] 1.1.1.1 - - [originalsite/sid#7f1ed9741948][rid#7f1ed69a40a0/initial] init rewrite engine with requested uri /user/login, referer: https://www.newsite/
[Sat Feb 08 18:08:37.574627 2025] [rewrite:trace3] [pid 723835] mod_rewrite.c(494): [client 1.1.1.1:52738] 1.1.1.1 - - [originalsite/sid#7f1ed9741948][rid#7f1ed69a40a0/initial] applying pattern ‘^/(.*)?$’ to uri ‘/user/login’, referer: https://www.newsite/
[Sat Feb 08 18:08:37.574644 2025] [rewrite:trace1] [pid 723835] mod_rewrite.c(494): [client 1.1.1.1:52738] 1.1.1.1 - - [originalsite/sid#7f1ed9741948][rid#7f1ed69a40a0/initial] pass through /user/login, referer: https://www.newsite/

Why is it pass through?

Any tip is appreciated.
Thank you!

Your current RewriteRule pattern is ^(.*)$ which captures the entire URI. The logs show it’s processing “/user/login” correctly (it’s being recognized). The RewriteConds look correct in syntax, but there might be a subtle issue.

The problem might be related to how the pattern matching is working. Let’s modify the rules to make them more explicit and add some debugging. Here’s a suggested fix:

# Add this line to enable detailed logging
RewriteEngine On
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3

# Modified rules
RewriteCond %{REQUEST_URI} !^/user/login
RewriteCond %{REQUEST_URI} !^/contactus
RewriteRule ^(.*)$ https://newsite/$1 [R=301,L]

Note the use of ^ at the start of the path in RewriteCond patterns. Also, turned on detail logging, to help future debugging.

The log entry you shared actually indicates the rule is working as intended - when it says “pass through /user/login”, it means it’s NOT redirecting that URL, which is what you want.

To verify this is working correctly, you should test these scenarios:

  1. /user/login - should NOT redirect
  2. /contactus - should NOT redirect
  3. /any/other/path - should redirect to https://newsite/any/other/path
1 Like

I tried !^/user/login no change.

To verify this is working correctly, you should test these scenarios:

1. `/user/login` - should NOT redirect
2. `/contactus` - should NOT redirect
3. `/any/other/path` - should redirect to https://newsite/any/other/path

This is correct.

Here is the full log per one request:

[rid#7f1ed69a40a0/initial] init rewrite engine with requested uri /user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] applying pattern '^/(.*)?$' to uri '/user/login', referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] pass through /user/login, referer: https://www.newsite/

[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] add path info postfix: /home/au/drupal/docroot/user -> /home/au/drupal/docroot/user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] strip per-dir prefix: /home/au/drupal/docroot/user/login -> user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] applying pattern '^' to uri 'user/login', referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] add path info postfix: /home/au/drupal/docroot/user -> /home/au/drupal/docroot/user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] strip per-dir prefix: /home/au/drupal/docroot/user/login -> user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] applying pattern '^' to uri 'user/login', referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] add path info postfix: /home/au/drupal/docroot/user -> /home/au/drupal/docroot/user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] strip per-dir prefix: /home/au/drupal/docroot/user/login -> user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] applying pattern '^' to uri 'user/login', referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] add path info postfix: /home/au/drupal/docroot/user -> /home/au/drupal/docroot/user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] strip per-dir prefix: /home/au/drupal/docroot/user/login -> user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] applying pattern '/\\.|^\\.(?!well-known/)' to uri 'user/login', referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] add path info postfix: /home/au/drupal/docroot/user -> /home/au/drupal/docroot/user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] strip per-dir prefix: /home/au/drupal/docroot/user/login -> user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] applying pattern '^' to uri 'user/login', referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] add path info postfix: /home/au/drupal/docroot/user -> /home/au/drupal/docroot/user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] strip per-dir prefix: /home/au/drupal/docroot/user/login -> user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] applying pattern '^core/install.php' to uri 'user/login', referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] add path info postfix: /home/au/drupal/docroot/user -> /home/au/drupal/docroot/user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] strip per-dir prefix: /home/au/drupal/docroot/user/login -> user/login, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] applying pattern '^' to uri 'user/login', referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] rewrite 'user/login' -> 'index.php', referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] add per-dir prefix: index.php -> /home/au/drupal/docroot/index.php, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] setting lastsub to rule with output index.php, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] strip document_root prefix: /home/au/drupal/docroot/index.php -> /index.php, referer: https://www.newsite/
[rid#7f1ed69a40a0/initial] [perdir /home/au/drupal/docroot/] internal redirect with /index.php [INTERNAL REDIRECT], referer: https://www.newsite/
[rid#7f1ed4a4a480/initial/redir#1] init rewrite engine with requested uri /index.php, referer: https://www.newsite/
[rid#7f1ed4a4a480/initial/redir#1] applying pattern '^/(.*)?$' to uri '/index.php', referer: https://www.newsite/
[rid#7f1ed4a4a480/initial/redir#1] rewrite '/index.php' -> 'https://newsiteindex.php', referer: https://www.newsite/
[rid#7f1ed4a4a480/initial/redir#1] explicitly forcing redirect with https://newsiteindex.php, referer: https://www.newsite/
[rid#7f1ed4a4a480/initial/redir#1] setting lastsub to rule with output https://newsite$1, referer: https://www.newsite/
[rid#7f1ed4a4a480/initial/redir#1] escaping https://newsiteindex.php for redirect, referer: https://www.newsite/
[rid#7f1ed4a4a480/initial/redir#1] copying destination=/investors/docs to query string for redirect , referer: https://www.newsite/
[rid#7f1ed4a4a480/initial/redir#1] redirect to https://newsiteindex.php?destination=/investors/docs [REDIRECT/301], referer: https://www.newsite/

My problem is following, I have an original site and a new site. The new site has a couple of links to old site with forms, and I want to keep the old site logic and the two pages, the first rewrite.

My conf file is:

<IfModule mod_ssl.c>
<VirtualHost *:443>
     ServerAdmin info@originalsite
     ServerName originalsite
     ServerAlias www.originalsite
     DocumentRoot /home/au/drupal/docroot
     AssignUserId au au
     Protocols h2 http/1.1
     RewriteEngine On
     LogLevel alert rewrite:trace3
     RewriteCond %{REQUEST_URI} !user/login
     RewriteCond %{REQUEST_URI} !contactus
     RewriteRule ^/(.*)?$ https://newsite$1 [R=301]
     <Directory /home/au/drupal/docroot>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
        RewriteBase /
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
     </Directory>
     Include /etc/letsencrypt/options-ssl-apache.conf
     SSLCertificateFile /etc/letsencrypt/live/originalsite/fullchain.pem
     SSLCertificateKeyFile /etc/letsencrypt/live/originalsite/privkey.pem
</VirtualHost>
</IfModule>

The last line of the log:

[rid#7f1ed4a4a480/initial/redir#1] redirect to https://newsiteindex.php?destination=/investors/docs [REDIRECT/301], referer: https://www.newsite/

The second rewriteRule kills the intent of what I want to do as seen in the log file.
What would be a way to get it done?
Thanks.

I solved my problems.
I have a newsite.com and an originalsite.com.
I want a couple of links from the newsite.com to point to oldsite.com pages and to keep their functionality.
I thought using URL (/user/login, contactus) would work but it does not.
Part of the problem is server logic and there were more rewrites happening…
Using HTTP_REFERER did the trick.

 RewriteCond %{HTTP_REFERER} !https://www.newsite.com/
 RewriteCond %{HTTP_REFERER} !https://newsite.com/
 RewriteCond %{HTTP_REFERER} !https://www.originalsite.com/
 RewriteCond %{HTTP_REFERER} !https://originalsite.com/
 RewriteRule ^/(.*)?$ https://newsite.com$1 [R=301,L]

If the referer is not one of the above then redirect.
If the referer is newsite or originalsite then do nothing.

This topic was automatically closed after 30 days. New replies are no longer allowed.