Forum Moderators: phranque

Message Too Old, No Replies

Rewrite index to "/" and non-www to www + subdomain

         

end0re

2:24 am on Jul 17, 2009 (gmt 0)

10+ Year Member



Basically I have on the main folder .htaccess written this:

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

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

And it works like this:

1. example.com
2. example.com/index.html
3. example.com/forum/
4. example.com/forum/index.html
5. forum.example.com
6. forum.example.com/index.html
7. forum.example.com/test/
8. forum.example.com/test/index.html
->
1. www.example.com
2. www.example.com
3. www.example.com/forum/
4. www.example.com/forum/
5. forum.example.com
6. www.example.com/forum/ ( >>WRONG<< )
7. forum.example.com/test/
8. www.example.com/forum/test/ ( >>WRONG<< )

So as you can see the index.html to "/" doesn't work on subdomains.
Can anyone please help me with this?

[edited by: jdMorgan at 4:34 am (utc) on July 17, 2009]
[edit reason] example.com [/edit]

jdMorgan

4:24 am on Jul 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




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

That should fix your problem, retaining the example.com/index.html -> www.example.com functionality.
Note that I removed the end anchors on all hostnames, so as to allow redirection of FQDN-format hostnames and hostnames with port numbers appended. An alternate method using an anchored pattern would be "^example\.com\.?(:[0-9]+)?$"

Replace the broken pipe "¦" character above with a solid pipe character before use; Posting on this forum modifies the pipe characters.

Jim

end0re

7:57 am on Jul 17, 2009 (gmt 0)

10+ Year Member



Emm...it's different now, but still it's wrong:

6. forum.example.com/index.html
8. forum.example.com/test/index.html
->
6. forum.example.com/forum/ ( >>WRONG<< )
8. forum.example.com/forum/test/ ( >>WRONG<< )

jdMorgan

3:20 pm on Jul 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is not due to the action of this code. Look for another rule in this file or in any .htaccess file in any directories above this one that is intended to internally rewrite the subdomain to a same-named subdirectory. Re-arrange the code so that this internal rewrite is executed *after* the redirects.

Rule execution order is critical, since you do not want your internally-rewritten filepaths exposed to the client as URLs by subsequent external redirects.

To avoid this and other unexpected effects, place all external redirects first, in order from most-specific patterns and conditions (fewest URLs affected) to least-specific (most URLs affected), followed by all internal rewrites, again in order from most-specific to least-specific patterns/conditions.

Jim

end0re

4:31 pm on Jul 17, 2009 (gmt 0)

10+ Year Member



There are no .htaccess files upper than the one I'm editing.
And in that .htaccess file isn't anything else.
Also in /forum/ & /forum/test/ doesn't contain .htaccess files.

Any suggestions?
P.S. I'm not familiar with .htaccess commands >.<

jdMorgan

4:55 pm on Jul 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There must be *some* mechanism in place to 'map' the subdomain URL "forum.example.com" to the server filepath "/forum", otherwise, you would not have or need a "/forum" subdirectory.

That mechanism and the redirects above are interfering with each other, and the cause of this interference must be found and fixed.

Our forum Charter contains links to resources about .htaccess and mod_rewrite -- See links at top of this page).

Jim