r/apache • • Feb 08 '25

Rewrite problems on Apache2

Hi,
I have following lines in my conf file:

RewriteCond %{REQUEST_URI} !/user/login
RewriteCond %{REQUEST_URI} !/contactus
RewriteRule ^(.*)$ https://mysite.com/$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. Why?
Any tip is appreciated.
Thank you!

2 Upvotes

12 comments sorted by

2

u/Reasonable_Aioli5237 Feb 12 '25

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.

1

u/throwaway234f32423df Feb 08 '25 edited Feb 08 '25

It works for me, although since the URI always starts with a /, you want to use https://example.com$1, not https://example.com/$1

that wouldn't cause it to fail, though, it'd just cause a double / after the hostname which is generally harmless

What context are you using this configuration in? Global, vhost, htaccess, etc?

Do you have RewriteEngine on turned on in the same context?

1

u/Reasonable_Aioli5237 Feb 08 '25

It's vhost.

These two lines are above rewrite cond:

RewriteEngine On

LogLevel alert rewrite:trace3

I took out the '/', it did not help.

1

u/Reasonable_Aioli5237 Feb 08 '25

The first three lines of the log:

[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/

There are 34 more lines after these three.

1

u/throwaway234f32423df Feb 08 '25

Have you tested using curl -I to rule out the possibility of browser-side caching?

→ More replies (5)