user1448031
user1448031

Reputation: 2166

htaccess redirecting all urls except one to a different domain not working

I'm trying to redirect all urls except http://shop.mysite.com/enquiry/ to a different domain like below but it's redirecting everything to "example.com", including the "enquiry" page.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/enquiry/
RewriteRule .* https://www.example.com [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^(.*)$ index.php
</IfModule>

So how do I make sure "enquiry" isn't redirected?

Upvotes: 0

Views: 398

Answers (2)

anubhava
anubhava

Reputation: 785156

For a negative condition, you should THE_REQUEST variable instead of REQUEST_URI:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} !/enquiry[?\s/] [NC]
RewriteRule ^ https://www.example.com [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^ index.php [L]
</IfModule>

THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1

Make sure to clear your browser cache or use a new browser when testing.

Upvotes: 1

athms
athms

Reputation: 958

Your final RewriteRule for index.php is probably causing an additional redirect to occur in the PHP script.

You could try moving the redirect below this rule:

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}/index.html !-f RewriteCond %{REQUEST_FILENAME}/index.php !-f RewriteRule ^(.*)$ index.php RewriteCond %{REQUEST_URI} !^/enquiry/? RewriteRule .* https://www.example.com [R=301,L] </IfModule>

In addition, you will need to add some more rewrite conditions to exclude referenced scripts/stylesheets from being redirected too.

Upvotes: 1

Related Questions