Tuesday, March 15, 2022

[FIXED] .htaccess RewriteRule conflict across different directories

Issue

On my Ubuntu development machine(s) I run a LAMP stack. For each website I work on, I create a new directory off root:

/var/www/somesite.com

/var/www/anothersite.com

The problem I have is that apache wont allow duplicate rewrite rules across these folders. For instance, If I set up this:

RewriteRule ^track/(.*)$ /somesite.com/order_track.php [nc,L]

http://localhost/somesite.com/track/abc123 - works as intented

This same declaration wont work on anothersite.com

RewriteRule ^track/(.*)$ /anothersite.com/order_track.php [nc,L]

http://localhost/anothersite.com/track/abc123 - Apache returns a 404.

Clearing browser cache and restarting Apache have no effect. Apache seems to "remember" the first like rewriterule used. This happens on all of my computers(Home, work, laptop)

Edit: I should have mentioned that I have an htaccess file in each directory. The root /var/www does not contain an htaccess file. Each directory's htaccess should operate independently. But they do not.


Solution

You can have this in /somesite.com/.htaccess:

RewriteEngine On
RewriteBase /somesite.com/

RewriteRule ^track/(.*)$ order_track.php [NC,L]

Then this in /anothersite.com/.htaccess:

RewriteEngine On
RewriteBase /anothersite.com/

RewriteRule ^track/(.*)$ order_track.php [NC,L]


Answered By - anubhava

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.