Redirect MSIE browse + ES and DE traffic

Hello,

This is my first post in SitePoint. I need help with something. Please help. I’m a amateur regarding htaccess,
so please bear with me as I try to explain what I need done.

I want to redirect all versions of IE browsers from one page to another internal page (not 301 redirect),
and also redirect all Spanish and German traffic/users to the same respective pages.
I have 4 specific pages that I need to do this on.

I did get some help over at webmasterworld.com/apache/ , but the problem is that the main guy running
that forum has a hectic time in life and can’t support the forum as much any more. So, here’s what I got:

# 302 Redirect MSIE requests for all .htm pages except for NEW.htm itself to NEW.htm
RewriteCond %{REQUEST_URI} !^/NEW\\.htm$
RewriteCond %{HTTP_USER_AGENT} ^([^\\ ]+\\ )+MSIE\\ [0-9]+\\.
RewriteRule \\.htm$ http://www.example.com/NEW.htm [R=302,L]
#
# 302 Redirect requests having German or Spanish as the FIRST language
# preference for any of these four specific pages to NEW.htm
RewriteCond %{HTTP:Accept-Language} ^(de|es)([,;\\-].+)?$ [NC]
RewriteRule ^(PAGE1|PAGE2|PAGE3|PAGE4)\\.htm$ http://www.example.com/NEW.htm [R=302,L]

Regarding the first code - Redirecting MSIE users:

The first part of this code redirect all MSIE users that arrive in this directory to NEW.htm

The problem here is that I have a lot of other pages in the directory, and I don’t want all these pages to be effected.
Only 4 of them. And these 4 should redirect (ONLY for MSIE users) to a similar page but different.

I want it done like this:
page1.htm to be redirected to page1.new.htm
page2.htm to be redirected to page2.new.htm
page3.htm to be redirected to page3.new.htm
page4.htm to be redirected to page4.new.htm

Regarding the second code - Redirecting ES and DE users:

The second part of this code redirect all ES and DE users that arrive at page1.htm, page2.htm, page3.htm and page4.htm in this directory to NEW.htm

But I want them to be redirected like this:
page1.htm to be redirected to page1.new.htm
page2.htm to be redirected to page2.new.htm
page3.htm to be redirected to page3.new.htm
page4.htm to be redirected to page4.new.htm

I was told that the command “RewriteCond” only applies to the next line, so I asked:

“Because the RewriteCond only applies to the first RewriteRule should I put in 4 RewriteCond lines,
and get rid of the {REQUEST_URI} line at the top since not all are going to get redirected to the same page?”

RewriteCond %{HTTP_USER_AGENT} ^([^\\ ]+\\ )+MSIE\\ [0-9]+\\.
RewriteRule ^(PAGE1)\\.htm$ http://www.example.com/page1.new.htm [R=302,L]
RewriteCond %{HTTP_USER_AGENT} ^([^\\ ]+\\ )+MSIE\\ [0-9]+\\.
RewriteRule ^(PAGE2)\\.htm$ http://www.example.com/page2.new.htm [R=302,L]
RewriteCond %{HTTP_USER_AGENT} ^([^\\ ]+\\ )+MSIE\\ [0-9]+\\.
RewriteRule ^(PAGE3)\\.htm$ http://www.example.com/page3.new.htm [R=302,L]
RewriteCond %{HTTP_USER_AGENT} ^([^\\ ]+\\ )+MSIE\\ [0-9]+\\.
RewriteRule ^(PAGE4)\\.htm$ http://www.example.com/page4.new.htm [R=302,L] 

… but I did not get any clear answer to this.

And I also asked regarding the second code:
“And the same goes for the {HTTP:Accept-Language} code, redirecting Spanish and German browsers as follows:”

RewriteCond %{HTTP:Accept-Language} ^(de|es)([,;\\-].+)?$ [NC]
RewriteRule ^(PAGE1)\\.htm$ http://www.example.com/page1.new.htm [R=302,L]
RewriteCond %{HTTP:Accept-Language} ^(de|es)([,;\\-].+)?$ [NC]
RewriteRule ^(PAGE2)\\.htm$ http://www.example.com/page2.new.htm [R=302,L]
RewriteCond %{HTTP:Accept-Language} ^(de|es)([,;\\-].+)?$ [NC]
RewriteRule ^(PAGE3)\\.htm$ http://www.example.com/page3.new.htm [R=302,L]
RewriteCond %{HTTP:Accept-Language} ^(de|es)([,;\\-].+)?$ [NC]
RewriteRule ^(PAGE4)\\.htm$ http://www.example.com/page4.new.htm [R=302,L] 

… but I did not get any clear answer to this either.

Can someone in here please help?

Hi, welcome to the SitePoint forums :wave:

The answer to both your questions is yes. Although you can use a trick to make your life a little bit easier by using multiple RewriteConds and the [OR] flag, and by using regular expressions in the RewriteRule, like so:


RewriteCond %{HTTP_USER_AGENT} ^([^\\ ]+\\ )+MSIE\\ [0-9]+\\. [OR]
RewriteCond %{HTTP:Accept-Language} ^(de|es)([,;\\-].+)?$ [NC]
RewriteRule ^page([1-4])\\.htm$ http://www.example.com/page$1.new.htm [R=302,L]

That does exactly the same as the 8 lines you had above :slight_smile:

The [OR] after the first RewriteCond says that either the first RewriteCond has to evaluate to true, or the second RewriteCond has to evaluate to true (or both), instead of the normal case where both have to evaluate to true.

In the RewriteRule I’ve put ([1-4]), which is a regular expression to match any character between 1 and 4 (inclusive). The parentheses here mean that I can use the value as a backreference in the second part of the RewriteRule. That’s what the $1 is in the new URL.

So if someone requests page1.htm they get page1.new.htm, and the same for 2, 3 and 4, but not for other numbers.

Hope that makes sense. If not, just say the word and I’ll try to explain further :slight_smile:

Thank you very much :slight_smile: Great answer.
So, I actually could use my 8 lines of code?

You gave a good answer but I understand now that I need to be very very very specific regarding these things.
I actually have no numbers in my htm-files. That was just for illustrating purpose. I thought that would make things easier for people to explain this to me. Sorry.
The actual filenames are:

bro.htm –> bro.new.htm
krone.htm –> krone.new.htm
protese.htm –> protese.new.htm
implantater.htm –> implantater.new.htm

Could you please put together a working code for me with these filenames :slight_smile:
Would be very much appreciated.

This should do it:


RewriteCond %{HTTP_USER_AGENT} ^([^\\ ]+\\ )+MSIE\\ [0-9]+\\. [OR]
RewriteCond %{HTTP:Accept-Language} ^(de|es)([,;\\-].+)?$ [NC]
RewriteRule ^(bro|krone|protese|implantater)\\.htm$ http://www.example.com/$1.new.htm [R=302,L]

:slight_smile:

Thank you so much :slight_smile:

An a last question:
If I do not want to redirect ES and DE users but only IE users (some times I only need that code),
then the code could look like this?

RewriteCond %{HTTP_USER_AGENT} ^([^\\ ]+\\ )+MSIE\\ [0-9]+\\.
RewriteRule ^(bro|krone|protese|implantater)\\.htm$ http://www.example.com/$1.new.htm [R=302,L]

I just removed the [OR] at the end of the “IE”-line
and removed the whole line with “Accept-Language”.
Would this be correct?

Yes, that would be correct :slight_smile: