Forum Moderators: phranque

Message Too Old, No Replies

htaccess redirect double check

         

wesg

8:42 pm on Jun 12, 2008 (gmt 0)

10+ Year Member



Hello Gurus,
I've been searching WebmasterWorld Forums for the solution, and have hammered out code that does 90% of what I want it to do.

My goal is to have all user agents iPod and iPhone (I'm making my site mobile) be redirected to sub.domain.com from domain.com. So far I have succeeded in having both user agents be redirected to sub.domain.com/index.php. I want to drop the index.php and have pages redirected to their respective subdomain page.

For example, http://www.example.com/contact/ should redirect to http://sub.example.com/contact/

Here is what I have so far, and I hope that the geniuses on this forum can help me figure out the rest.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (.*iPod.*¦.*iPhone.*) [NC]
RewriteRule ^(.*) http://sub.example.com/$1? [R=301,L]
</IfModule>

This is the .htaccess file in my main domain, by the way.

[edited by: jdMorgan at 1:55 am (utc) on June 13, 2008]
[edit reason] example.com [/edit]

g1smd

8:49 pm on Jun 12, 2008 (gmt 0)

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



In what way is it failing for URLs that you do want to redirect, and for URLs that you do not want to redirect?

You do not need the .* patterns in the USER_AGENT test.

You can use iPod¦iPhone or even iP(od¦hone) just fine.

wesg

9:06 pm on Jun 12, 2008 (gmt 0)

10+ Year Member



By failing I mean it's not detecting the extra address on the script.

That means that a request for http://www.example.com/contact redirects to http://sub.example.com/index.php instead of http://sub.example.com/contact.

[edited by: jdMorgan at 1:54 am (utc) on June 13, 2008]
[edit reason] example.com [/edit]

g1smd

9:09 pm on Jun 12, 2008 (gmt 0)

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



Flush your browser cache, because it should work just fine.

You also need to test with inputs of both www.domain.com/contact and domain.com/contact too.

wesg

9:17 pm on Jun 12, 2008 (gmt 0)

10+ Year Member



Flushed the cache, still no dice. It must be something simple wrong.

jdMorgan

9:30 pm on Jun 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something simple? Not necessarily... You've got an internal rewrite to index.php being applied first, and then the code posted here is "exposing" that rewrite. You'll need to move the code posted here so that it is applied *before* the code that does that internal rewrite to the script. That might be easy or it might be impossible, depending on how your hosting is set up and whether you're using a "control panel" or a host-managed script installer.

Jim

wesg

9:37 pm on Jun 12, 2008 (gmt 0)

10+ Year Member



I found something interesting. I am using pretty permalinks on my wordpress blog, and I found this in the subdomain htaccess. Perhaps this is the problem?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

g1smd

11:36 am on Jun 13, 2008 (gmt 0)

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



Yes it is.

You need to get the redirect in, before the rewrite.

wesg

9:07 pm on Jun 13, 2008 (gmt 0)

10+ Year Member



I think I know what I have to do now. I have to add a condition to the subdomain that says only do the rewrite like that if the user is coming from my main domain. How could I do that?

g1smd

9:15 pm on Jun 13, 2008 (gmt 0)

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



Changing the order will do what you want to do.

wesg

9:30 pm on Jun 13, 2008 (gmt 0)

10+ Year Member



I see your point. However, the htaccess files I"m talking about are two separate files. One is in the main domain which handles the redirect, and the second is in the subdomain, which makes the wordpress permalinks pretty. Is there a way to do it in one file?