Forum Moderators: phranque
[edited by: jdMorgan at 6:53 pm (utc) on Sep. 30, 2008]
[edit reason] Please use example.com only [/edit]
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]
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
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
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
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]
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.
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]
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
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]
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]
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.
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]
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.
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]
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.
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
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.
... 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.