garethhallnz
garethhallnz

Reputation: 327

htaccess conditions

Need some advanced redirecting with a .htaccess rule

This is my current rule.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/content/newzealand/mpc/mpc_newzealand_website/en/home_mpc/van.*
RewriteCond %{REQUEST_URI} !^/content/newzealand/mpc/mpc_newzealand_website/en/home_mpc/truck.*
RewriteCond %{REQUEST_URI} !^/admin.*
RewriteCond %{REQUEST_URI} !^/login.*
RewriteCond %{REQUEST_URI} !^/session.*
RewriteCond %{REQUEST_URI} !^/path_maps.*
RewriteCond %{REQUEST_URI} !.*\.(gif|jpg|png|js|css|swf)

RewriteCond %{HTTP_HOST} !^www\.dealer1\.com
RewriteCond %{HTTP_HOST} !^www\.dealer2\.com
RewriteCond %{HTTP_HOST} !^www\.dealer3\.com
RewriteCond %{HTTP_HOST} !^www\.dealer4\.com


RewriteRule (.*) http://www.example.com [R=301,L]

This rule directs all traffic not in the RewriteCond to www.example.com. However I now need the following amendment, if this:

/content/newzealand/mpc/mpc_newzealand_website/en/home_mpc/van.*

path gets visited then I want it to redirect the www.example.com/van

Upvotes: 0

Views: 89

Answers (2)

TerryE
TerryE

Reputation: 10878

The first thing that I would do is to simply this. First adding .* at the end of a pattern is redundant, and if you want to test if a URI ends with .swf say then you use \.swf$. Also combine rules where sensible.

Next the simplest way to do a blanked exclusion is to use a "^ -", which is just a shorthand for "always do nothing" (if the conds are satisfied). Adding an [L] means that the following rules don't get used. Alternatively you can skip the next N rules by using [skip=N].

And specify a base, e.g. / if the .htaccess is in DOCROOT, So:

Options +FollowSymLinks
RewriteEngine On
RewriteBase   /
#
# No redirects for a specific list of dealers, paths and file types
#
RewriteCond %{HTTP_HOST}   ^www\.(dealer1|dealer2|dealer3|dealer4)\.com [OR]
RewriteCond %{REQUEST_URI} ^/(admin|login|session|path_maps)\b          [OR]
RewriteCond %{REQUEST_URI} \.(gif|jpg|png|js|css|swf)^
RewriteRule ^              -                                            [L]
#
# Otherwise redirect var URIs to /van
#
RewriteCond %{REQUEST_URI} ^/content/newzealand/mpc/mpc_newzealand_website/en/home_mpc/van
RewriteRule ^              http://www.domain.com/van                    [R=301,L]
#
# Redirect everything except truck to /
#
RewriteCond %{REQUEST_URI} !^/content/newzealand/mpc/mpc_newzealand_website/en/home_mpc/truck.*
RewriteRule ^              http://www.domain.com                        [R=301,L]

Lastly, I am really suspicious of your long URIs. The request URI everything excluding the server and query bits (e.g. for http//server/abc/login?user=fred it is /abc/login). For example you really want to redirect everything other this path at this site to the new domain?

http://www.domain.com/content/newzealand/mpc/mpc_newzealand_website/en/home_mpc/truck

Forcing users to use such complex URIs is a good way to discourage their visiting!

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143856

After your current rule, just add this:

RewriteCond %{HTTP_HOST} !^www\.dealer1\.com
RewriteCond %{HTTP_HOST} !^www\.dealer2\.com
RewriteCond %{HTTP_HOST} !^www\.dealer3\.com
RewriteCond %{HTTP_HOST} !^www\.dealer4\.com

RewriteRule ^/?content/newzealand/mpc/mpc_newzealand_website/en/home_mpc/van.* http://www.domain.com/van [R=301,L]

Upvotes: 0

Related Questions