Redirect Doesn't Work

Greetings,

I want to redirect visitors if the URL doesn’t match. I’m trying this:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(https?:\\/\\/)?(w{3}\\.)?popupsinc\\.com\\/intranet\\/?$ [NC]
RewriteRule (.*) http://www.custompopups.com
</IfModule>

However, this redirects everything. Can anyone see what I’m doing wrong? I suspect it might be my rule patten “(.*)”, but I don’t know how to change/fix this.

Thanks,
Shane.

%{HTTP_HOST} only contains the host name, nothing else. So it does not (like you seem to assume) contain the schema (http/https) and also does not contain the requested path.

What you need to do is forget about http/https, and spread the host and path over %{HTTP_HOST} and %{REQUEST_URI}, like so


RewriteCond %{HTTP_HOST} !^(w{3}\\.)?popupsinc\\.com$ [NC,OR]
RewriteCond %{REQUEST_URI} !^/intranet/?$ [NC]
RewriteRule .? http://www.custompopups.com/ [L,R=301]

Note that I’ve removed the <IfModule mod_rewrite.c>…</IfModule> since they are quite abusive of the server (why ask for something over and over again when you already know the answer?)

Also, I added [L,R=301] to the last rule to make it a 301 (permanent) redirect instead of the default 302 (temporary).

OK, I got it working:


RewriteEngine on
RewriteCond %{REQUEST_URI} !^/intranet([/\\w\\.-]*)*\\/?$ [NC]
RewriteRule .* http://www.custompopups.com

This redirects everything except visits to the intranet subdirectory. I also see that slashes didn’t need to be escaped. Thanks for the tip.

Do you think the “RewriteEngine on” directive is necessary?

Thanks,
Shane.

Yup. Without the RewriteRule doesn’t work at all :slight_smile: