Forum Moderators: phranque

Message Too Old, No Replies

Redirect a page in htaccess based on O\S

Redirect a page in htaccess based on O\S

         

LeClerc

5:56 pm on Jul 8, 2010 (gmt 0)

10+ Year Member



Hi there,

I'm looking for some kind person to help me with something that on the face of it seems quite simple, but I just can't get it to work!

I would like to redirect a single page to another page, if the O\S used (to access the first page) is a Macintosh.

From general searching and reading, I've come up with a folder redirect:

RewriteCond %{HTTP_USER_AGENT} Macintosh [NC]
RewriteRule ^gallery/(.*)$ /gallery-mac/$1 [L]


Which should, technically redirect if the accessing URL is http://www.example.com/gallery/video-clips to http://www.example.com/gallery-mac/video-clips and the user is using a Macintosh - however doesn't seem to work for me.

What I actually want to do is this:

Redirect http://www.example.com/gallery/video-clips to http://www.example.com/gallery-mac/video-clips if you're on a Macintosh (but the entire folder redirect would have been a good start!).

Please would anyone be kind enough to point me in the right direction?

Regards
Alan.

g1smd

8:52 pm on Jul 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It doesn't redirect. It's a rewrite.

The user should see the content located within the other folder, returned at the URL they originally requested.

For a redirect, you'd need to include the domain name in the target and the [R=301,L] flags.

LeClerc

5:40 am on Jul 9, 2010 (gmt 0)

10+ Year Member



Hi g1smd, thanks for your reply.

It is a rewrite that I'm trying to achieve - I want the requested URL to stay the same, but the content returned from the other folder (or ideally on a page-for-page basis).

Apologies for mixing terminologies - it's definately a rewrite and not a redirect.

LeClerc

8:31 pm on Jul 9, 2010 (gmt 0)

10+ Year Member



I now know where the "problem" lies - I just like a few pointers on how to fix it, if it's possible please?

The actual rewrite bit is fine - it's not working because of the Joomla core SEF section above it. The page URL's never get to my bit of rewrite, as it matches the code above it first, which is terminated by an [L] flag.

The code is thus:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.shtml|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]


RewriteCond %{HTTP_USER_AGENT} Windows [NC]
RewriteRule ^gallery/([^/.]+)/?$ gallery-mac/$1 [L]


Therefore, the URL matches the first Rewrite Rule and stops thereafter.

The code is obviously there to make user-friendly URL's on the site - is there any way to incorporate what I want to do in it?

Is it a futile task?!

Many thanks in advance.
Alan.

g1smd

9:35 pm on Jul 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to move your code to be first, so your code is used for those URL requests, then the following code is used for all the other URL requests.

Do not make the trailing slash optional. Set it so that it is either required, or must not be present. Allowing both URLs to work is a Duplicate Content problem. Redirect requests for the non-canonical URL to the canonical form with yet another rule ahead of all of the other rules.

Use RewriteRule for all of the rules. The redirect will need the [R=301,L] flags.