Forum Moderators: phranque

Message Too Old, No Replies

htaccess and subdomains

htaccess and subdomains not redirecting properly.

         

dmaynard2

12:11 pm on Dec 13, 2007 (gmt 0)

10+ Year Member



I apologize in advance for this "newsbie" question.

I have added this piece of code to my .htaccess file with the hope of redirecting mysite.com to www.mysite.com

########## Begin - 301 Redirect
#
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST}!^www\.mysite\.com$
RewriteRule ^(.*)$ [mysite.com...] [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.php\ HTTP/
RewriteRule index\.html$ [mysite.com...] [R=301,L]
#
########## End - 301 Redirect

This works fine, with the exception that now my subdomains such as blog.mysite.com now redirect to a 404 error page.

What am I doing wrong?

jdMorgan

3:24 pm on Dec 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What's wrong:
1) Your code explicitly redirects requests for *any* subdomain that is not www, as specified by the RewriteCond checking "!^www\.example\.com$"
2) You are using copied-and-pasted code without finding out how it works first. Since this code changes your server configuration and may severely damage your site if it doesn't suit your needs precisely, that's really not a very good idea.
3) Your rules are not in the correct order. Always put the most specific external redirect rules first, followed by the less-specific ones, then the most-specific internal rewrite rules followed by the least-specific.
4) Do not end-anchor hostnames. Or if you do, you must provide for an optional port number appended to the hostname.

########## Begin - 301 Redirects
#
RewriteEngine on
RewriteBase /
#
# Redirect client requests for "/index.html" to "/" in same directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.php(\?[^\ ]*)?\ HTTP/
RewriteRule index\.html$ http://www.example.com/%1 [R=301,L]
#
# Redirect requests for example.com to corresponding page on www.example.com
RewriteCond %{HTTP_HOST} ^example\.com(:[0-9]+)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
########## End - 301 Redirects

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

dmaynard2

12:14 am on Dec 14, 2007 (gmt 0)

10+ Year Member



Thank you that worked perfectly.

g1smd

11:26 pm on Dec 16, 2007 (gmt 0)

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



I am surprised that "it worked perfectly" as there is a typo in the code.

On one line it tests for index.php and on another for index.html instead.

.

I have seen this longer code that caters for most known index names:

# Index redirection
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.(html?地spx?夸spx?圭gi如l如hp[2-5]圭fm)[^\ ]*\ HTTP/

RewriteRule ^(([^/]*/)*)index\.(html?地spx?夸spx?圭gi如l如hp[2-5]圭fm)$ http://www.example.com/$1? [R=301,L]

There are multiple ways to get the job done.