Forum Moderators: phranque

Message Too Old, No Replies

.htaccess and Subdomain issues

.htaccess breaks subdomain

         

elanio

4:43 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



Trying to figure out why the subdomain results in server not found when .htaccess is in place.

Here is my code:

Options +FollowSymLinks
RewriteEngine On
AddType x-mapp-php5 .php

#declares the base from which to start the rewrite
RewriteBase /

#sets the folder in which to make the 'base'
#RewriteCond %{REQUEST_URI} !(0)
#RewriteRule ^0/(.*)$ 0/$1/ [L]

#allows direct access to this directory
RewriteCond %{REQUEST_URI} !(staging/0)
RewriteRule ^staging/(.*)$ staging/0/$1/ [L]

#allows direct access to this directory for a seperate domain
RewriteCond %{REQUEST_URI} !(joe)
RewriteRule ^joe(.*)$ joe/$1/ [L]

#allows direct access to this subdomain
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
RedirectMatch 301 ^/subdomain/(.*)$ [subdomain.example.com...]

#sets the conditions.
RewriteCond %{REQUEST_URI} ^/*
RewriteCond %{REQUEST_URI} !(0¦1¦staging/0¦thoughts¦mobile¦joe¦subdomain)
RewriteRule ^(.*)$ 0/$1 [L,NC]

#custom error pages.
errorDocument 400 /error/400.html
errorDocument 401 /error/401.html
errorDocument 403 /error/403.html
errorDocument 404 /error/404.html
errorDocument 500 /error/500.html

jdMorgan

5:01 pm on Oct 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For start, add a slash between ".com" and "$1" in the substitution URL of your first external redirect rule (r=301), change the RedirectMatch to a RewriteRule, and put both of those redirects first in your list of rules.

There are several more tweaks needed, but these should not directly affect the problem.

Jim

elanio

5:24 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



Thanks for the quick reply. I believe I made the requested changes you outlined for me.

The below updated .htaccess file is giving me server error. (500)

Options +FollowSymLinks
RewriteEngine On
AddType x-mapp-php5 .php

#declares the base from which to start the rewrite
RewriteBase /

#allows direct access to this subdomain
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RedirectRule 301 ^/subdomain/(.*)$ [subdomain.example.com...]

#sets the folder in which to make the 'base'
#RewriteCond %{REQUEST_URI} !(0)
#RewriteRule ^0/(.*)$ 0/$1/ [L]

#allows direct access to this directory
RewriteCond %{REQUEST_URI} !(staging/0)
RewriteRule ^staging/(.*)$ staging/0/$1/ [L]

#allows direct access to this directory for a seperate domain
RewriteCond %{REQUEST_URI} !(joe)
RewriteRule ^joe(.*)$ joe/$1/ [L]

#sets the conditions.
RewriteCond %{REQUEST_URI} ^/*
RewriteCond %{REQUEST_URI} !(0¦1¦staging/0¦thoughts¦mobile¦joe¦subdomain)
RewriteRule ^(.*)$ 0/$1 [L,NC]

#custom error pages.
errorDocument 400 /error/400.html
errorDocument 401 /error/401.html
errorDocument 403 /error/403.html
errorDocument 404 /error/404.html
errorDocument 500 /error/500.html

g1smd

5:26 pm on Oct 8, 2009 (gmt 0)

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



change the RedirectMatch to a RewriteRule

You have several typos in that line.

RedirectRule 301 ^/subdomain/(.*)$ http: //subdomain.example.com/$1

This is a long way from being correct.

elanio

6:08 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



Could you highlight some of the typos? I am not seeing them.

Thanks for any help, it's greatly appreciated.

elanio

8:10 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



RewriteRule 301 ^/subdomain/(.*)$ [subdomain.example.com...]

does not work.

jdMorgan

10:46 pm on Oct 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A quick check of that line against the examples given in the Apache mod_rewrite documentation [httpd.apache.org], or even comparing it against your other redirect RewriteRule should make the problem more obvious.

Jim

elanio

10:48 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



when I comment out this:

#sets the conditions.
#RewriteCond %{REQUEST_URI} ^/*
#RewriteCond %{REQUEST_URI} !(0¦1¦joe¦subdomain)
#RewriteRule ^(.*)$ 0/$1 [L,NC]

Everything works... sorta.

My server is set up as such:

www.example.com
/0
/1
/joe
/subdomain (this is the folder that the subdomain sets as it's root)

in my cpanel the subdomain.example.com is setting /subdomain as its destination

My site lives inside of /0 & /1.

When the above is commented out, my site runs off the root '/'
When it is not commented it runs off the set base folder '/0' or '/1' (depending on which I have it set to).

However, subdomain.example.com it says, "The page isn't redirecting properly"

I am so confused as to what is going on..

elanio

11:03 pm on Oct 8, 2009 (gmt 0)

10+ Year Member



Thanks for checking back Jim. I solved it.

RewriteRule ^subdomain/ - [L]

placed at the top, prevents the mod-rewrite from occuring on the folder.

so subdomain.example.com works perfectly since it was mapped to the folder:

http://www.example.com/subdomain

jdMorgan

12:57 am on Oct 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How did you fix the redirect we discussed previously?

--- different subject ---

The second RewriteCond in your commented-out code should have prevented it from rewriting your subdomain unless the 'pipe' characters in your pattern are broken pipe "¦" characters, as always shown on this forum.

Be aware that any broken pipe character you see here (or copy from here) must be replaced by a solid pipe character before use; Your patterns won't work if that is not done.

See the second RewriteCond below. Those "¦" need to be replaced.
#sets the conditions.
#RewriteCond %{REQUEST_URI} ^/*
#RewriteCond %{REQUEST_URI} !(0¦1¦joe¦subdomain)
#RewriteRule ^(.*)$ 0/$1 [L,NC]

Jim

elanio

4:27 pm on Oct 12, 2009 (gmt 0)

10+ Year Member



Here is what I now have for my site with the sub domain re-direct.

However, when going to my site sans 'www' it automatically goes to my sub domain. Not sure how to correct this. See below for what I have.

Options +FollowSymLinks
RewriteEngine On
AddType x-mapp-php5 .php

#allows direct access to this subdomain
#stops the mod re-write from happening on this folder
RewriteRule ^subdomain/ - [L]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ [subdomain\.example\.com...] [L,R=301]
RedirectMatch 301 ^/ariel/(.*)$ [subdomain.example.com...]

#declares the base from which to start the rewrite
RewriteBase /

#sets the folder in which to make the 'base'
RewriteCond %{REQUEST_URI} !(0)
RewriteRule ^0/(.*)$ 0/$1/ [L]

#allows direct access to this directory
RewriteCond %{REQUEST_URI} !(staging/0)
RewriteRule ^staging/(.*)$ staging/0/$1/ [L]

#allows direct access to this directory
RewriteCond %{REQUEST_URI} !(example2)
RewriteRule ^example2(.*)$ example2/$1/ [L]

#sets the conditions.
RewriteCond %{REQUEST_URI} ^/*
RewriteCond %{REQUEST_URI} !(0¦1¦staging/0¦mobile¦example2¦subdomain)
RewriteRule ^(.*)$ 0/$1 [L,NC]

#custom error pages.
errorDocument 400 /error/400.html
errorDocument 401 /error/401.html
errorDocument 403 /error/403.html
errorDocument 404 /error/404.html
errorDocument 500 /error/500.html

g1smd

6:20 pm on Oct 12, 2009 (gmt 0)

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



Why do you still have a Redirectmatch line in there?

You need to change that rule to use RewriteRule not Redirectmatch (as discussed way above).

elanio

7:44 pm on Oct 12, 2009 (gmt 0)

10+ Year Member



changing it to a RewriteRule causes a 500 error.

elanio

7:46 pm on Oct 12, 2009 (gmt 0)

10+ Year Member



correction, I forgot to remove 301 when changing it to RewriteRule.

Same issues still applies though, going to www.example.com and example.com do not take you to the same place.

http://example.com -> goes to [subdomain.example.com...]
http://www.example.com -> goes to http://www.example.com

jdMorgan

8:36 pm on Oct 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have a rule in there which clearly redirect example.com to subdomain.example.com instead of redirecting example.com to www.exmaple.com, so I'm not sure why you're asking about that here.

Jim

elanio

3:58 pm on Oct 13, 2009 (gmt 0)

10+ Year Member



I am such a n00b. Thanks for all the help everybody. This is definitely a worth while learning experience. I did not see the re-direct until you pointed it out jdMorgan.

Thanks again everyone!