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?

Sarao

6:17 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



You mean there will be another rule? If so, what should I add?

Sarao

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

10+ Year Member



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

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

jdMorgan

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

WebmasterWorld Senior Member 10+ Year Member



I'm going to suggest a change in approach, both because it simplifies the rules and because it will "look much better" to visitors and search engines.

Do not externally redirect to "/home" for this specific domain. Instead, internally rewrite to that directory. If you do that, then all the other rules become much simpler.

If you don't do that, then you will need up to six rules to fix everything, and every rule that you might add in the future may also probably need to be 'doubled' because you will have to handle both the www.example.com/ case and the www.example.com/home/ case for each rule's intended function.

Try this:


RewriteEngine on
#
# Externally redirect direct client requests for "index.htm" in any directory to "/" in
# that same directory, fixing the domain canonicalization as well
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.htm\ HTTP/ [NC]
RewriteRule ^(([^/]*/)*)index\.htm$ http://www.example.com/$1 [NC,R=301,L]
#
# Externally redirect requests for any resource at example.com (or at www.example.com with any
# uppercase letters, appended periods, or port numbers) to same resource at www.example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Internally rewrite requests for www.example.com/<anything> to
# www.example.com/[b]home[/b]/<anything> unless it has already been done.
RewriteCond RewriteCond %{HTTP_HOST} ^www.example\.com
RewriteCond $1 !^home/
RewriteRule (.*) /home/$1 [L]

Note that I simplified the index page redirect, since only index.htm is being used on this site. I also added logic to redirect requests for www.example.com that contain uppercase letters, trailing dots, or port numbers to the canonical www.example.com hostname. For example, a perfectly-valid but non-canonical request for www.Example.com.:80/foo.htm will now be redirected to www.example.com/foo.htm

This approach does not expose the "/home/" directory path to visitors or search engines, but will allow all the files for that domain to be moved into the /home subdirectory, while still appearing to be in the root directory of www.example.com when seen by the client.

Jim

Sarao

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

10+ Year Member



He, thanx for such a great help. I have just used that code, but it isnt working. Its not redirecting to /home I have replaced all the example.com with the domain i want to use

[edited by: Sarao at 8:09 pm (utc) on Sep. 30, 2008]

Sarao

8:26 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



one more thing, is it possible to redirect all example/index.htm requess to another page? Coz root have some other stuff, which I dont want to get access when example.com is used.

I am really asking very much of questions, bt I am sorry i have to and thanx for all of your great help.

g1smd

8:53 pm on Sep 30, 2008 (gmt 0)

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



Yes, of course it is possible, but all of the other code has to be designed to not interact with that.

I think you need to look carefully at the whole picture and be very clear as to exactly happens for all domains and all folders on the server, before proceeding any further.

jdMorgan

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

WebmasterWorld Senior Member 10+ Year Member



And also, read the replies here very carefully. I stated above that the code I posted will NOT externally redirect to /home, it will internally rewrite to /home, and I gave you a very good reason to do it that way...

People are far less motivated to reply if there are indications that their replies are not being read and understood, or if those replies are taken as having little value because a contract and payment have not been required...

Jim

Sarao

7:20 am on Oct 1, 2008 (gmt 0)

10+ Year Member



Morgan, Yes I know what internally redirection mean. but Sir that isnt working. I dont know why, but showed me the root contents when I updated the .htaccess file.

jdMorgan

2:05 pm on Oct 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm afraid it's up to you to modify the code to suit your hostnames, determine that the code is installed in the proper location, and test it. There are hundreds of potential minor problems that can stop the code from working, and the job of debugging falls to you. From a practical standpoint, it can be impossible to support such a debugging effort in a forum context.

Please report your test results and error log contents, and ask single, specific questions for best results. You might consider hiring a local expert to help you with this if you need the problem solved quickly.

Jim

Sarao

6:27 pm on Oct 7, 2008 (gmt 0)

10+ Year Member



Hi,
I am using this code (sorry for the domain name here) and I am getting internal server error.


RewriteEngine on

# Externally redirect direct client requests for "index.htm" in any directory to "/" in
# that same directory, fixing the domain canonicalization as well
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.htm\ HTTP/ [NC]
RewriteRule ^(([^/]*/)*)index\.htm$ http://www.example.com/home/$1 [NC,R=301,L]

# Externally redirect requests for any resource at example.com (or at www.example.com with any
# uppercase letters, appended periods, or port numbers) to same resource at www.example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/home/$1 [R=301,L]

# Internally rewrite requests for www.example.com/<anything> to
# www.example.com/home/<anything> unless it has already been done.
RewriteCond RewriteCond %{HTTP_HOST} ^www.example\.com
RewriteCond $1 !^home/
RewriteRule (.*) /home/$1 [L]

This code is w hat you have suggested

[edited by: jdMorgan at 9:15 pm (utc) on Oct. 8, 2008]
[edit reason] example.com [/edit]

Sarao

6:29 pm on Oct 7, 2008 (gmt 0)

10+ Year Member



One thing I want to add is, /home is my second name and root is another domain which have files for the root domain

g1smd

6:43 pm on Oct 7, 2008 (gmt 0)

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



What are these two lines supposed to do? Can you explain your thoughts?

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

They don't look like they will do what you expect of them (but I am not sure what that is).

The good news is that the correction is likely quite easy.

.

This is where your "Internal Server Error" typo resides:

RewriteCond [i]RewriteCond[/i] %{HTTP_HOST} ^www[b]\.[/b]example\.com

It is nearly always a simple typo somewhere.

I see the "Internal Server Error" message dozens of times per week, sometimes dozens of times per day, or even hour.

Sarao

8:22 pm on Oct 7, 2008 (gmt 0)

10+ Year Member



Thanx, I just came here to post that I got it, the typo. But you have already replied. I dont know why there are these links: RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$

I just copied them from the morgans' post, because I dont know Apache.

One thing now, since I have another domain which shows the root files. I want to make it from non-www to www version with the above code which I am currently using to redirect to /home directory

I tried using this

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301]

it works fine with example domain but when i go to [another doamin], it doesnt show up anything. Just say cannot find server.

[edited by: Sarao at 8:38 pm (utc) on Oct. 7, 2008]

[edited by: jdMorgan at 9:29 pm (utc) on Oct. 7, 2008]
[edit reason] No personal URLs, please. Use example.com only. [/edit]

jdMorgan

9:32 pm on Oct 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"Cannot find server" indicates that your DNS record for the new domain is misconfigured or undefined. Edit your DNS zone file to correct the problem.

Be aware that in order for your "account" to receive requests for a different/new domain, you must either have a unique (non-shared) IP address, or you must have an arrangement with your hosting service to deliver these requests to your existing virtual server.

Jim

Sarao

7:10 am on Oct 8, 2008 (gmt 0)

10+ Year Member



no no! I just checked using the Hader Checker Tool. when I Use this

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301]

# Externally redirect direct client requests for "index.htm" in any directory to "/" in
# that same directory, fixing the domain canonicalization as well
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.htm\ HTTP/ [NC]
RewriteRule ^(([^/]*/)*)index\.htm$ http://www.example.com/home/$1 [NC,R=301,L]

# Externally redirect requests for any resource at example.com (or at www.example.com with any
# uppercase letters, appended periods, or port numbers) to same resource at www.example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/home/$1 [R=301,L]

# Internally rewrite requests for www.example.com/<anything> to
# www.example.com/home/<anything> unless it has already been done.
RewriteCond RewriteCond %{HTTP_HOST} ^www.example\.com
RewriteCond $1 !^home/

It is working for example.com. But when I goto example, it redirects as hxxp:// example .com /home/www.example.com/

thats why it says cannot find server.

[edited by: Sarao at 7:13 am (utc) on Oct. 8, 2008]

[edited by: jdMorgan at 9:17 pm (utc) on Oct. 8, 2008]
[edit reason] example.com [/edit]

Sarao

7:10 am on Oct 8, 2008 (gmt 0)

10+ Year Member



no no! I just checked using the Header Checker Tool. when I Use this

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301]

# Externally redirect direct client requests for "index.htm" in any directory to "/" in
# that same directory, fixing the domain canonicalization as well
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.htm\ HTTP/ [NC]
RewriteRule ^(([^/]*/)*)index\.htm$ http://www.example.com/home/$1 [NC,R=301,L]

# Externally redirect requests for any resource at example.com (or at www.example.com with any
# uppercase letters, appended periods, or port numbers) to same resource at www.example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/home/$1 [R=301,L]

# Internally rewrite requests for www.example.com/<anything> to
# www.example.com/home/<anything> unless it has already been done.
RewriteCond RewriteCond %{HTTP_HOST} ^www.example\.com
RewriteCond $1 !^home/

It is working for example.com. But when I goto example, it redirects as hxxp:// example .com /home/www.example.com/

thats why it says cannot find server.

[edited by: Sarao at 7:12 am (utc) on Oct. 8, 2008]

[edited by: jdMorgan at 9:18 pm (utc) on Oct. 8, 2008]
[edit reason] example.com [/edit]

g1smd

7:22 am on Oct 8, 2008 (gmt 0)

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



You might want to put "code" tags round your code, and change the domain to "example.com". Please re-edit one of the posts above.

You have missed the final RewriteRule off your example by the look of things. Can you paste that in?

You still have the double RewriteCond RewriteCond in the code that you posted.

Which of your two posts is the correct one? You might want to re-edit one of them to say just "duplicate", so the forum moderator can remove it.

.

Before going any further you need to fix the order of your code blocks.

All of the redirects must be first.

Redirects must be from most specific (i.e. works on one specific page, or specific pattern), to least specific (such as the "fixes all non-www to www" redirect).

The rewrites must be listed after the redirects. This is to stop the internal server path being exposed to the user.

This needs to be fixed first; irrespective of what else does and does not work.

Sarao

9:18 am on Oct 8, 2008 (gmt 0)

10+ Year Member



OK I am sorry, but I am not able to edit the post. I am posting the code again, and request moderators to kindly delete the above two posts.

RewriteEngine on 

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^$ http://www.example.com/$1 [R=301]

# Externally redirect direct client requests for "index.htm" in any directory to "/" in
# that same directory, fixing the domain canonicalization as well
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.htm\ HTTP/ [NC]
RewriteRule ^(([^/]*/)*)index\.htm$ http://www.example2.com/home/$1 [NC,R=301,L]

# Externally redirect requests for any resource at example.com (or at www.example.com with any
# uppercase letters, appended periods, or port numbers) to same resource at www.example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.example2\.com$
RewriteRule (.*) http://www.example2.com/home/$1 [R=301,L]

# Internally rewrite requests for www.example.com/<anything> to
# www.example.com/home/<anything> unless it has already been done.
RewriteCond %{HTTP_HOST} ^www.example2\.com
RewriteCond $1 !^home/
RewriteRule (.*) /home/$1 [L]

The above code, when I use, works fine for the example.com, it changes it to www.example.com But when I go for itsaphotog, it shows "Cannot find Server". On checking HTTP Headers, it shows following error:

HTTP/1.1 301 Moved Permanently =>
Date => Wed, 08 Oct 2008 09:17:56 GMT
Server => Apache
Location => http://www.example2.com/home/http://www.example.com//
Connection => close
Content-Type => text/html; charset=iso-8859-1

[edited by: Sarao at 9:43 am (utc) on Oct. 8, 2008]

[edited by: jdMorgan at 9:20 pm (utc) on Oct. 8, 2008]
[edit reason] example.com [/edit]

g1smd

9:35 am on Oct 8, 2008 (gmt 0)

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



As at 09:34 the order is still wrong. Your 1 - 2 - 3 - 4 should be 3 - 2 - 1 - 4.

You need a # on each blank line for the forum code tag to do what it needs to do.

The non-www to www redirect must be the last redirect of the redirects. If it isn't, then it exposes the internal server path of your following rewrites.

The redirect for trailing periods looks malformed. In particular:

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

looks like it could never work. What is it supposed to do?

Don't forget to "escape" any periods in URLs in your conditions and rules.

The redirects must all be before the rewrites. Be careful of that when you move things around. The rewrites must be last.

Where you have a blank line in your code put a # so that the forum code tag does what it should for all of the code.

Change the domain name to example.com so that moderators don't have to spend time doing it.

You say there are two domain names involved. In that case there should also be various canonical fixes for both domain names in the rules - but I don't see that. Make sure that the # notes explain what the code does and that the URLs match. Use exampleone and exampletwo if you have to.

You get 60 minutes to edit a post, then it is locked.

Sarao

9:48 am on Oct 8, 2008 (gmt 0)

10+ Year Member



I have changed the domain name to example2.com

It should be l like this? anything more I have to do?

RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com [NC] 
RewriteCond %{HTTP_HOST} !^www\.example2\.com$
RewriteRule (.*) http://www.example2.com/home/$1 [R=301,L]
#
# Externally redirect direct client requests for "index.htm" in any directory to "/" in
# that same directory, fixing the domain canonicalization as well
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.htm\ HTTP/ [NC]
RewriteRule ^(([^/]*/)*)index\.htm$ http://www.example2.com/home/$1 [NC,R=301,L]
#
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^$ http://www.example.com/$1 [R=301]
#
# Internally rewrite requests for www.example2.com/<anything> to
# www.example2.com/home/<anything> unless it has already been done.
RewriteCond %{HTTP_HOST} ^www.example2\.com
RewriteCond $1 !^home/
RewriteRule (.*) /home/$1 [L]

[edited by: Sarao at 9:49 am (utc) on Oct. 8, 2008]

[edited by: jdMorgan at 12:48 pm (utc) on Oct. 8, 2008]
[edit reason] De-linked "example2" [/edit]

g1smd

9:53 am on Oct 8, 2008 (gmt 0)

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



Fix up and expand the notes.

Some of the notes say "example", but the following rule says "example2".

There is "index" fixing for only one domain. You surely need both domains to be fixed.

There is non-www to www fixing for only one domain. You surely need both (and please add notes to those rules).

Some of the rules don't have a note.

jdMorgan

12:57 pm on Oct 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> The redirect for trailing periods looks malformed.

But it isn't malformed. Note the [NC] and no end-anchor on the first RewriteCond.

The purpose of the two RewriteConds together is to say, "If the requested hostname is *any* variation (appended period or port number, or uppercase/lowercase) of example2.com or www.example2.com, but is not exactly "www.example2.com", then redirect it to exactly "www.example2.com"

The first RewriteCond is only necessary here because we don't want to invoke the rule for example.com, we only want to invoke it for example2.com

A bit tricky, that! ;)

Jim

g1smd

3:43 pm on Oct 8, 2008 (gmt 0)

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



Got it. I stared at it for a while, but gave up. It appeared to me to be testing that it is something and is not the same thing - at the same time. The NC and the word "exactly" were critical in the understanding. Additionally, it is only fixing stuff on the end of the hostname, whereas I was tuned into fixing errors on the end of any type of URL request and therefore I was expecting to see THE_REQUEST on one of those two lines. Don't mind me. Carry on...

g1smd

5:37 pm on Oct 8, 2008 (gmt 0)

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



OK. At risk of misunderstanding something else, but...

The second rule is a redirect such that index file requests redirect to: www.example2.com/home/ - I don't think it should be redirecting to a folder.

The fourth rule is a rewrite from www.example2.com to the path at /home/ - and that rule does fit what you said you wanted to do.

With both in place, I would think they might clash. I am not sure if they can cause an infinite loop.

Sarao

6:34 pm on Oct 8, 2008 (gmt 0)

10+ Year Member



yup, its putting it on loop as /home/home.htm/home something this way

g1smd

6:50 pm on Oct 8, 2008 (gmt 0)

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



Knock the /home part off the target URL of the redirect in the second block of code. You'll still need the /$1 on the end.

... and knock the /home part off the target URL of the first rule too. Again, retain the /$1 part on the end.

Please also add comments to all of the rules that don't have comments.
You need to be able to understand what each line does six months from now.

.

Is your index file actually called home.htm rather than the more common index.html?

If it is, then that is an added complication that should have been disclosed right at the beginning, because it is possible that when checking for the folder home the file home.html is matched instead, and/or vice-versa.

This 56 message thread spans 2 pages: 56