Forum Moderators: phranque

Message Too Old, No Replies

Help with htaccess (redirect)

         

seb2point0

10:16 am on Aug 8, 2008 (gmt 0)

10+ Year Member



I am trying to set up my htaccess file to do the following.

1. Add www to the domain if not inputted by user,
2. Send any requests to www.example.com to www.example.com/blog (including the already changed URLs from http://example.com).

I would like the /blog redirect only to work when the root is accessed so I can still have access to any subfolders to the root. (ie: /form).

I was able to get the first part working but not the second. Can anyone help me out?

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

...

[edited by: jdMorgan at 3:42 pm (utc) on Aug. 9, 2008]
[edit reason] example.com [/edit]

jdMorgan

3:42 pm on Aug 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like you'll just need a second rule to internally rewrite root URL request (.htaccess regex "^$" ) to /blog/.

Jim

seb2point0

2:47 pm on Aug 11, 2008 (gmt 0)

10+ Year Member



What would that look like? I tried the following and it didn't work. I really need to get a book on htaccess...

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^$ http://www.example.eu/folder/

seb2point0

3:37 pm on Aug 11, 2008 (gmt 0)

10+ Year Member



nevermind.. i got it

securehotel

4:37 pm on Aug 11, 2008 (gmt 0)

10+ Year Member



Seb2.0, can you post it for others to learn from?thanks

seb2point0

6:36 pm on Aug 11, 2008 (gmt 0)

10+ Year Member



Sure:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^$ http://www.example.com/folder/

With this code, wheather the user types www.example.com or example.com, the www will be added. The last line redirects to /folder

g1smd

6:55 pm on Aug 11, 2008 (gmt 0)

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



*** The last line redirects to /folder ***

No it doesn't. It redirects to /folder/

The last line should be a rewrite to /folder/ so you should remove the domain name from that last line, and add [L] to the end.

[edited by: jdMorgan at 8:46 pm (utc) on Aug. 11, 2008]
[edit reason] clarified text. [/edit]

seb2point0

7:38 pm on Aug 11, 2008 (gmt 0)

10+ Year Member



Perhaps, but it still works for me

g1smd

7:55 pm on Aug 11, 2008 (gmt 0)

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




Maybe. Maybe not.

Mod_Rewrite does exactly what it is programmed do, not what you might have intended it to do.

Have you used a HTTP Header Checker to see what is going on?

jdMorgan

7:58 pm on Aug 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The last line is a 302-Moved Temporarily redirect to www.example.com/folder/ by default, since it includes the http method and the domain, but does not specify a particular redirect response using an [R=status] flag.

In addition, any RewriteRules following this one will be executed before the redirect takes place, since the [L] flag is missing.

Be sure this is what you want.

Jim

g1smd

7:59 pm on Aug 12, 2008 (gmt 0)

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



I am hoping the OP is aware of the dangers of back-to-back redirects, especially when one is a 301 redirect and the other is a 302 redirect.

If not, I'll recommend reading the many various things that Tedster has already written on this, and closely-related topics, over the last few years.

seb2point0

7:57 am on Aug 13, 2008 (gmt 0)

10+ Year Member



So then would it be better to have 2 conditions something like this?
RewriteCond %{HTTP_HOST} ^www\.example\.eu [NC]
RewriteRule ^$ /deedee/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^example\.eu [NC]
RewriteRule (.*) www.example.eu/deedee/$1 [L,R=301]

the first condition works but not the second?

[edited by: jdMorgan at 5:32 pm (utc) on Aug. 13, 2008]
[edit reason] examplified URLs [/edit]

g1smd

7:29 pm on Aug 13, 2008 (gmt 0)

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



I never like the root of a site to redirect to a deep internal URL on the same domain. It just feels wrong.

In what way does your code not work? What did it do? What did you expect it to do? How does that differ from what you expected?

I assume you have tested it with a variety of URL formats, both for root and deep-URL requests, and for both of those for both www and non-www URLs.

securehotel

9:07 pm on Aug 18, 2008 (gmt 0)

10+ Year Member



from wat i understand, [L] just means it's the last one, so it will stop looking for more rewrite rules.

so try this way,

RewriteCond %{HTTP_HOST} ^www\.example\.eu [NC]
RewriteRule ^$ /deedee/$1 [R=301]

RewriteCond %{HTTP_HOST} ^example\.eu [NC]
RewriteRule (.*) www.example.eu/deedee/$1 [L,R=301]

As for redirecting to a lower directory, it is common parctice and also if you get a "corrupted" folder, its easier to wipe on efolder than your whole webspace