Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess exception rules not working

These are my current rules in .htaccess. Basically, I want to redirect my entire site from domain1.com to domain2.com except for the following conditions:

  1. all files/pages inside /offers folder

  2. the page at /page/company-a

When I access www.domain1.com/page/company-a, I am redirected to domain2.com/index.php instead.

Any advice appreciated.

RewriteCond %{REQUEST_URI} !^/offers
RewriteCond %{REQUEST_URI} !^/page/company-a

RewriteCond %{HTTP_HOST}  ^www\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]


# Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
like image 964
Slay Avatar asked Jun 05 '26 03:06

Slay


1 Answers

Use THE_REQUEST variable instead of REQUEST_URI. THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.

RewriteEngine On

RewriteCond %{THE_REQUEST} !\s/+(offers|page/company-a) [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain1\.com$ [NC]
RewriteRule ^ http://www.domain2.com%{REQUEST_URI} [L,R=301,NE]

# Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]

Make sure to clear your browser cache while testing.

like image 155
anubhava Avatar answered Jun 07 '26 19:06

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!