Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite to redirect to a directory

mod_rewrite to redirect to a directory

         

Sarao

7:34 pm on Sep 29, 2008 (gmt 0)

10+ Year Member



Hi,

I want to redirect my domain to a directory /home

What I want is, either http://www.example.com is accessed or http://example.com it should be redirected to www.example.com/home

How can I do that?

g1smd

7:50 pm on Sep 29, 2008 (gmt 0)

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



Do you want the user to see the new URL, or still see the old one?

One is a redirect, the other is a rewrite. The code is slightly different.

Sarao

7:01 am on Sep 30, 2008 (gmt 0)

10+ Year Member



I want it like user sees that he has been redirected.

Sarao

7:02 am on Sep 30, 2008 (gmt 0)

10+ Year Member



I want it like user sees that he has been redirected.

g1smd

7:40 am on Sep 30, 2008 (gmt 0)

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



That's one line of very standard code that gets posted several time every day in this forum.

Check the previous threads here and post your best effort code as a basis for discussion.

However, I will note that redirecting your root URL to a deep internal page is not the best of things to be doing.

You will also need to check both www and non-www responses, and what happens when you include the index file filename in the request - those others also need to redirect, otherwise you are serving the same page of content at multiple URLs.

Sarao

8:13 am on Sep 30, 2008 (gmt 0)

10+ Year Member



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

I'm using this rewrite code, but when I go directly to www, it stay on root.

[edited by: jdMorgan at 6:48 pm (utc) on Sep. 30, 2008]
[edit reason] example.com [/edit]

g1smd

8:43 am on Sep 30, 2008 (gmt 0)

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



Using that rule, you are redirecting for all (.*) URLs (so, not just "/" that you wanted) that are non-www (see RewriteCond).

You needed both www and non-www, so you can omit the domain check.

Just as (.*) means all URLs, ^$ means only "/" root.

g1smd

8:46 am on Sep 30, 2008 (gmt 0)

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



You will also need a variation of the above rule, substituting $1 for /home/ so that all non-www URLs are redirected to the www version. That rule must be last.

Just before that "catch all" rule, you will need one to strip off the index file filename if one is requested.

So, three (at least) rules in all, and their order is important.

You don't need the line

RewriteBase / 
as that is the default anyway.

Sarao

9:05 am on Sep 30, 2008 (gmt 0)

10+ Year Member



I am not really not getting that.. Can you please tell what rule should be used?

g1smd

9:18 am on Sep 30, 2008 (gmt 0)

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



You need three rules, using the instructions above.

Which bit didn't you understand?

Sarao

9:26 am on Sep 30, 2008 (gmt 0)

10+ Year Member



Actually the problem is, I really dont know about all this rewrite stuff. That rule which I have written, I copied it from somewhere. So its confusing for me to edit that rule. I am sorry for the same. But I really cant do that myself.

g1smd

9:34 am on Sep 30, 2008 (gmt 0)

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



Delete the
RewriteBase /
line. You don't need it.

Put the

RewriteEngine On
line at the very top of the page, above the rules.

Copy the two-line rule out twice so you have two copies of it.

First copy:
Delete the

RewriteCond
line completely.
Change
(.*)
to be
^$

Second copy:
Change

/home/
to be
/$1

Finally, go fetch the redirect for named index files that redirects back to "/" rule that has been posted every day this week, and insert it between those two rules.

Post back here when done.

Sarao

9:42 am on Sep 30, 2008 (gmt 0)

10+ Year Member



OK, m doing it just right now. One more thing, you suggested me to delete the cond, I want to tell you, that there are two domains hosted on the same account. Thats why I am doing the mod_rewrite. I should have told this before.

Sarao

9:47 am on Sep 30, 2008 (gmt 0)

10+ Year Member



RewriteEngine on
RewriteCond %{HTTP_HOST} www.\example\.com$ [NC]
RewriteRule ^$ http://www.example.com/home/$1 [R=301,L]

I have done this code, and I think its doing right.

[edited by: jdMorgan at 6:50 pm (utc) on Sep. 30, 2008]
[edit reason] Please use example.com [/edit]

Sarao

9:55 am on Sep 30, 2008 (gmt 0)

10+ Year Member



Nopes, sorry thats not working. Now if I refer to www, it redirects, but if go to non-ww, it still stays there

g1smd

9:56 am on Sep 30, 2008 (gmt 0)

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



You have only done a third of the job. As explained above, there is additional stuff you need.

Additionally, your rule only now works for the www version, because it only tests for www.yoursite.com URLs.

Change what you have above so that

www.\
becomes
(www\.)?

Note that your code has .\ when it should be \. and that you must add the ( and ) and ? to it.

This stuff does exactly what you type and is completely unforgiving of typos and misunderstandings.

Finally, you will need to look at the other rules I suggested. To not do so is letting your site serve Duplicate Content.

[edited by: g1smd at 10:04 am (utc) on Sep. 30, 2008]

Sarao

10:01 am on Sep 30, 2008 (gmt 0)

10+ Year Member



you mean it should be like this?

RewriteCond %{HTTP_HOST} (www\.)?example\.com$ [NC]

[edited by: jdMorgan at 6:50 pm (utc) on Sep. 30, 2008]
[edit reason] example.com [/edit]

g1smd

10:07 am on Sep 30, 2008 (gmt 0)

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



Yes.

Now take a look at the other two rules that you will need to add to do the job properly.

One is to fix things so that index filename requests have the "index" filename stripped off - that code is in many other threads here.

The other is to redirect all non-www requests to www - I posted how to do that, above.

Sarao

12:09 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



I got the last thing, the www and non-www issue, but can't the second part, the index one. What does that mean?

g1smd

12:20 pm on Sep 30, 2008 (gmt 0)

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



domain.com/folder/
www.domain.com/folder/
domain.com/folder/index.html
www.domain.com/folder/index.html
are four URLs for the same content.

You will want to list

www.domain.com/folder/
and redirect the others.

Sarao

12:39 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



is this OK. Not the complete rule list, index one is still pening.

RewriteEngine on
RewriteCond %{HTTP_HOST} (www\.)?example\.com$ [NC]
RewriteRule ^$ http://www.example.com/home/$1 [R=301,L]

g1smd

12:45 pm on Sep 30, 2008 (gmt 0)

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



You need to take the $ off the end of the first line and the $1 off the end of the second line. Both are not needed.

Please review all the previous instructions. It looks like you are mixing things up between the different rules.

g1smd

1:19 pm on Sep 30, 2008 (gmt 0)

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



For the index stuff, see the sixth block of code (of nine blocks) in the example here:
[webmasterworld.com...]

Sarao

2:39 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



Okk.. I copy all that to my .htaccess?

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.(html?Šphp)(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]*/)*)index\.(html?Šphp)$ http://www.example.com/$1 [R=301,L]

g1smd

3:15 pm on Sep 30, 2008 (gmt 0)

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



Yes. That is the second of the three rules.

Change "example" to be your site's domain.

jdMorgan

4:07 pm on Sep 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I suggest you review that rule, look up each directive in the Apache mod_rewrite documentation, and analyze the regular expressions patterns so that you understand how the code works. Please do not copy and try to use code unless you completely understand how it works and what it does. Doing so can lead to unexpected side-effects and problems with your site's function and with search engine ranking.

Feel free to post specific questions here -- Our purpose here is to help you learn, not to simply provide cut-and-paste code.

Jim

Sarao

4:47 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



Thanx Morgan. I am doing that way.

@g1smd, I have my index page as index.htm and not html, so I should change that to htm in the code?

g1smd

4:52 pm on Sep 30, 2008 (gmt 0)

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



The ? in the html? part makes the L optional so the code works for both html and htm automatically.

Sarao

5:11 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



RewriteEngine on
RewriteCond %{HTTP_HOST} (www\.)?example\.com$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.(html?Šphp)(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]*/)*)index\.(html?Šphp)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^$ http://www.example.com/home/$1 [R=301,L]

is it fine now?

[edited by: jdMorgan at 6:52 pm (utc) on Sep. 30, 2008]
[edit reason] example.com [/edit]

g1smd

5:28 pm on Sep 30, 2008 (gmt 0)

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



No.

Lines 1 - 2 - 3 - 4 - 5 should be 1 - 3 - 4 - 2 - 5

But that's still only two of the rules you need.

Lose the $1 from the end of the URL in line 5.

You seem to have lost the general "non-www" to "www" rule that needs to go after all of this. That's an extra two lines to go on the end.

This 56 message thread spans 2 pages: 56