Forum Moderators: phranque

Message Too Old, No Replies

[htaccess] Advanced dynamic subdomains

         

Gerwin7

5:13 pm on Aug 13, 2005 (gmt 0)

10+ Year Member



I've got the following:

RewriteEngine on
RewriteBase /
Options -MultiViews

RewriteCond %{HTTP_HOST}!^www\.domain\.nl [NC]
RewriteRule (.*) [domain.nl...] [R=301,L]

# Archiefmappen
RewriteRule ^([0-9]+)$ /$1/ [R=302]
RewriteRule ^([0-9]+)/$ /do.php?page=$1 [L]

# Categoriemappen
RewriteRule ^([a-z]+)/([a-z]+)$ /$1/$2/ [R=302]
RewriteRule ^([a-z]+)/([a-z]+)/$ /domap.php?map1=$1&map2=$2 [L]
RewriteRule ^([a-z]+)$ /$1/ [R=302]
RewriteRule ^([a-z]+)/$ /domap.php?map1=$1 [L]

Now i want to be able to have subdomain's. The subdomain's must not have the other reqriterules that are listed but thay must always redirect to a slash as now. They must have own redirects; for example [a-z] to /domap-subdomain.php?map1=...

Gerwin7

11:38 pm on Aug 13, 2005 (gmt 0)

10+ Year Member



I have this now, but it does not seem to work i get 500 errors on the subdomains and the maps arent working...

RewriteEngine on
RewriteBase /
Options -MultiViews

# If no-www domain requested, externally redirect to www domain
RewriteCond %{HTTP_HOST} ^domain\.nl
RewriteRule (.*) [domain.nl...] [R=301,L]

# If www+subdomain domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.nl
RewriteRule (.*) [%1.domain.nl...] [R=301,L]

# If subdomain+www domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.domain\.nl
RewriteRule (.*) [%1.domain.nl...] [R=301,L]

# 0..9 maps rewrite
RewriteCond %{HTTP_HOST}!^www\.domain\.nl
RewriteRule ^([0-9]+)$ /$1/ [R=302]
RewriteRule ^([0-9]+)/$ /do.php?page=$1 [L]

# two maps rewrite
RewriteCond %{HTTP_HOST}!^www\.domain\.nl
RewriteRule ^([a-z]+)/([a-z]+)$ /$1/$2/ [R=302]
RewriteRule ^([a-z]+)/([a-z]+)/$ /domap.php?map1=$1&map2=$2 [L]

# one map reqrite
RewriteRule ^([a-z]+)$ /$1/ [R=302]
RewriteRule ^([a-z]+)/$ /domap.php?map1=$1 [L]

# we got subdomain
RewriteCond %{HTTP_HOST}!^www\.domain\.nl
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
RewriteRule (.*) /sd_%1/$1 [L]

Gerwin7

11:59 pm on Aug 13, 2005 (gmt 0)

10+ Year Member



It works now but i still get errors on the subdomain thing 500 error.

I have:

RewriteEngine on
RewriteBase /
Options -MultiViews

# If no-www domain requested, externally redirect to www domain
RewriteCond %{HTTP_HOST} ^domain\.nl
RewriteRule (.*) [domain.nl...] [R=301,L]

# If www+subdomain domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.nl
RewriteRule (.*) [%1.domain.nl...] [R=301,L]

# If subdomain+www domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.domain\.nl
RewriteRule (.*) [%1.domain.nl...] [R=301,L]

# 0..9
# RewriteCond %{HTTP_HOST}!^www\.domain\.nl
RewriteRule ^([0-9]+)$ /$1/ [R=302]
RewriteRule ^([0-9]+)/$ /do.php?page=$1 [L]

# Maps
RewriteRule ^([a-z]+)/([a-z]+)$ /$1/$2/ [R=302]
RewriteRule ^([a-z]+)/([a-z]+)/$ /domap.php?map1=$1&map2=$2 [L]
RewriteRule ^([a-z]+)$ /$1/ [R=302]
RewriteRule ^([a-z]+)/$ /domap.php?map1=$1 [L]

# Subdomain
RewriteCond %{HTTP_HOST}!^www\.domain\.nl
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
RewriteRule (.*) /sd_%1/$1 [L]

jdMorgan

12:04 am on Aug 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll just provide one example:

# 0..9 maps rewrite
RewriteCond %{HTTP_HOST} !^www\.domain\.nl
RewriteRule ^([0-9]+)$ http://%{HTTP_HOST}/$1/ [R=301,L]
#
RewriteCond %{HTTP_HOST} !^www\.domain\.nl
RewriteRule ^([0-9]+)/$ /do.php?page=$1 [L]

1) A RewriteCond applies only to the single RewriteRule following it.
2) If [R=30x] isused, a canonical URL should be provided.
3) Never use [R=302] unless the move is Temporary, and you have a planned date to remove the redirect.
4) A space is required between "}" and "!". Posting on this board removes them.

Jim

Gerwin7

1:11 am on Aug 14, 2005 (gmt 0)

10+ Year Member



OK, i'm getting somewhere.

I have this now:

RewriteEngine on
RewriteBase /
Options -MultiViews

# If no-www domain requested, externally redirect to www domain
RewriteCond %{HTTP_HOST} ^domain\.nl
RewriteRule (.*) [domain.nl...] [R=301,L]

# If www+subdomain domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.nl
RewriteRule (.*) [%1.domain.nl...] [R=301,L]

# If subdomain+www domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.domain\.nl
RewriteRule (.*) [%1.domain.nl...] [R=301,L]

# 0..9
RewriteCond %{HTTP_HOST}!^www\.domain\.nl
RewriteRule ^([0-9]+)$ [domain.nl...] [R=302]
RewriteCond %{HTTP_HOST}!^www\.domain\.nl
RewriteRule ^([0-9]+)/$ /do.php?page=$1 [L]

# Maps
RewriteCond %{HTTP_HOST}!^www\.domain\.nl
RewriteRule ^([a-z]+)/([a-z]+)$ [domain.nl...] [R=302]
RewriteCond %{HTTP_HOST}!^www\.domain\.nl
RewriteRule ^([a-z]+)/([a-z]+)/$ /domap.php?map1=$1&map2=$2 [L]
RewriteCond %{HTTP_HOST}!^www\.domain\.nl
RewriteRule ^([a-z]+)$ [domain.nl...] [R=302]
RewriteCond %{HTTP_HOST}!^www\.domain\.nl
RewriteRule ^([a-z]+)/$ /domap.php?map1=$1 [L]

# Subdomain
#RewriteCond %{HTTP_HOST}!^www\.domain\.nl
#R#ewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
#RewriteRule (.*) /sd_%1/$1 [L]

The problem is in the subdomain. I want this to happen:

[domain.nl...] --> [domain.nl...]
[domain.nl...] --> [domain.nl...]
[domain.nl...] --> [domain.nl...]

[domain.nl...] --> [domain.nl...]
[domain.nl...] --> [domain.nl...]
[domain.nl...] --> /do.php?page=1

[domain.nl...] --> [domain.nl...]
[domain.nl...] --> [domain.nl...]
[domain.nl...] --> /domap.php?map1=word

[domain.nl...] --> [domain.nl...]
[domain.nl...] --> [domain.nl...]
[domain.nl...] --> domap.php?map1=word&map2=two

[sub.domain.nl...] --> [sub.domain.nl...]
[sub.domain.nl...] --> [sub.domain.nl...]
[sub.domain.nl...] --> /sd_sub/domap.php?map1=word
[sub.domain.nl...] --> [sub.domain.nl...]
[sub.domain.nl...] --> /sd_sub/domap.php?map1=word&map2=two

[sub.domain.nl...] --> [sub.domain.nl...]
[sub.domain.nl...] --> [sub.domain.nl...]
[sub.domain.nl...] --> /sd_sub/do.php?page=1

Of course the "word" is a [a-z] and "two" is a [a-z], the number 1 is [0-9], and "sub" is [a-z0-9].

In short:
1) there must always be a slash if it's not a file itself (302)
2) first maps /[0-9]/ must point to a do.php file on every subdomain
3) first maps /[a-z]/ must point to a domap.php file on every subdomain
4) second maps /[a-z]/[a-z]/ must point to a domap.php file on every subdomain
5) if there is no subdomain precent (the directory doesnt exist, server must respond with a 404 error code.

Gerwin7

1:43 am on Aug 14, 2005 (gmt 0)

10+ Year Member



I've got the following now, it seems to work, is this the correct way? Is there a simpler way?

RewriteEngine on
RewriteBase /
Options -MultiViews

# If no-www domain requested, externally redirect to www domain
RewriteCond %{HTTP_HOST} ^domain\.be
RewriteRule (.*) [domain.be...] [R=301,L]

# If www+subdomain domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.be
RewriteRule (.*) [%1.domain.be...] [R=301,L]

# If subdomain+www domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.domain\.be
RewriteRule (.*) [%1.domain.be...] [R=301,L]

# 0..9 (WWW add slash)
RewriteCond %{HTTP_HOST} ^www\.domain\.be
RewriteRule ^([0-9]+)$ [domain.be...] [R=302]

# 0..9 (WWW rewrite)
RewriteCond %{HTTP_HOST} ^www\.domain\.be
RewriteRule ^([0-9]+)/$ /do.php?page=$1 [L]

# 0..9 (SUB add slash)
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.be
RewriteRule ^([0-9]+)$ [%1.domain.be...] [R=302]

# 0..9 (SUB rewrite)
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.be
RewriteRule ^([0-9]+)/$ /sd_%1/do.php?page=$1 [L]

# map1 (WWW add slash)
RewriteCond %{HTTP_HOST} ^www\.domain\.be
RewriteRule ^([a-z]+)$ [domain.be...] [R=302]

# map1 (WWW rewrite)
RewriteCond %{HTTP_HOST} ^www\.domain\.be
RewriteRule ^([a-z]+)/$ /domap.php?map1=$1 [L]

# map1 (SUB add slash)
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.be
RewriteRule ^([a-z]+)$ [%1.domain.be...] [R=302]

# map1 (SUB rewrite)
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.be
RewriteRule ^([a-z]+)/$ /sd_%1/domap.php?map1=$1 [L]

# map2 (WWW add slash)
RewriteCond %{HTTP_HOST} ^www\.domain\.be
RewriteRule ^([a-z]+)/([a-z]+)$ [domain.be...] [R=302]

# map2 (WWW rewrite)
RewriteCond %{HTTP_HOST} ^www\.domain\.be
RewriteRule ^([a-z]+)/([a-z]+)/$ /domap.php?map1=$1&map2=$2 [L]

# map2 (SUB add slash)
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.be
RewriteRule ^([a-z]+)/([a-z]+)$ [%1.domain.be...] [R=302]

# map2 (SUB rewrite)
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.be
RewriteRule ^([a-z]+)/([a-z]+)/$ /sd_%1/domap.php?map1=$1&map2=$2 [L]

jdMorgan

1:44 am on Aug 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Again, DO NOT use 302. It may cause huge problems [google.com]. If the URL is missing a slash, it is always wrong, and should be Permanently redirected with a 301 to the same URL with a trailing slash.

Jim

Gerwin7

2:14 am on Aug 14, 2005 (gmt 0)

10+ Year Member



Ok i've changed the 302 to 301. I have the following now:

RewriteEngine on
RewriteBase /
Options -MultiViews

# If no-wwwdomain requested, externally redirect to www domain
RewriteCond %{HTTP_HOST} ^domain\.nl
RewriteRule (.*) [domain.nl...] [R=301,L]

# If www+sub requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.nl
RewriteRule (.*) [%1.domain.nl...] [R=301,L]

# If subdomain+www domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.domain\.nl
RewriteRule (.*) [%1.domain.nl...] [R=301,L]


#### SOME PROBLEM HERE IF PEOPLE JUST REQUESTING [sub.domain.nl...]

# index (SUB add slash)
# RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
# RewriteRule ^$ [%1.domain.nl...] [R=301]

# index (SUB rewrite)
# RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
# RewriteRule ^$ /subdomains/%1/index.php?sub=%1&page=$1 [L]


# 0..9 (WWW add slash)
RewriteCond %{HTTP_HOST} ^www\.domain\.nl
RewriteRule ^([0-9]+)$ [domain.nl...] [R=301]

# 0..9 (WWW rewrite)
RewriteCond %{HTTP_HOST} ^www\.domain\.nl
RewriteRule ^([0-9]+)/$ /do.php?page=$1 [L]

# 0..9 (SUB add slash)
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
RewriteRule ^([0-9]+)$ [%1.domain.nl...] [R=301]

# 0..9 (SUB rewrite)
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
RewriteRule ^([0-9]+)/$ /subdomains/%1/do.php?sub=%1&page=$1 [L]

# map1 (WWW add slash)
RewriteCond %{HTTP_HOST} ^www\.domain\.nl
RewriteRule ^([a-z]+)$ [domain.nl...] [R=301]

# map1 (WWW rewrite)
RewriteCond %{HTTP_HOST} ^www\.domain\.nl
RewriteRule ^([a-z]+)/$ /domap.php?map1=$1 [L]

# map1 (SUB add slash)
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
RewriteRule ^([a-z]+)$ [%1.domain.nl...] [R=301]

# map1 (SUB rewrite)
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
RewriteRule ^([a-z]+)/$ /subdomains/%1/domap.php?sub=%1&map1=$1 [L]

# map2 (WWW add slash)
RewriteCond %{HTTP_HOST} ^www\.domain\.nl
RewriteRule ^([a-z]+)/([a-z]+)$ [domain.nl...] [R=301]

# map2 (WWW rewrite)
RewriteCond %{HTTP_HOST} ^www\.domain\.nl
RewriteRule ^([a-z]+)/([a-z]+)/$ /domap.php?map1=$1&map2=$2 [L]

# map2 (SUB add slash)
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
RewriteRule ^([a-z]+)/([a-z]+)$ [%1.domain.nl...] [R=301]

# map2 (SUB rewrite)
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
RewriteRule ^([a-z]+)/([a-z]+)/$ /subdomains/%1/domap.php?sub=%1&map1=$1&map2=$2 [L]

I have some problem still with the normal requests.
Is it correct to do it this way? Can it be simpeler?

Gerwin7

7:42 pm on Aug 14, 2005 (gmt 0)

10+ Year Member



I cant seem to get it working correct. I think the problem is in the domap and do on subdomains. That can be a filename or a directory. I've dropped that. I want to rewrite maps on maindomain but i want to have subdomains as a real subdomain (files and directory's on the subdomain must be accesable)

I have this:

RewriteEngine on
RewriteBase /
Options -MultiViews

# If no-wwwdomain requested, externally redirect to www domain
RewriteCond %{HTTP_HOST} ^domain\.nl
RewriteRule (.*) [domain.nl...] [R=301,L]

# If www+sub requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.nl
RewriteRule (.*) [%1.domain.nl...] [R=301,L]

# If subdomain+www domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.domain\.nl
RewriteRule (.*) [%1.domain.nl...] [R=301,L]


# 0..9 (WWW add slash)
RewriteCond %{HTTP_HOST} ^www\.domain\.nl
RewriteRule ^([0-9]+)$ [domain.nl...] [R=301]

# 0..9 (WWW rewrite)
RewriteCond %{HTTP_HOST} ^www\.domain\.nl
RewriteRule ^([0-9]+)/$ /do.php?page=$1 [L]

# map1 (WWW add slash)
RewriteCond %{HTTP_HOST} ^www\.domain\.nl
RewriteRule ^([a-z]+)$ [domain.nl...] [R=301]

# map1 (WWW rewrite)
RewriteCond %{HTTP_HOST} ^www\.domain\.nl
RewriteRule ^([a-z]+)/$ /domap.php?map1=$1 [L]

# map2 (WWW add slash)
RewriteCond %{HTTP_HOST} ^www\.domain\.nl
RewriteRule ^([a-z]+)/([a-z]+)$ [domain.nl...] [R=301]

# map2 (WWW rewrite)
RewriteCond %{HTTP_HOST} ^www\.domain\.nl
RewriteRule ^([a-z]+)/([a-z]+)/$ /domap.php?map1=$1&map2=$2 [L]

# map2 (SUB add slash)
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
RewriteRule ^([a-z]+)/([a-z]+)$ [%1.domain.nl...] [R=301]

# map2 (SUB rewrite)
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
RewriteRule ^([a-z]+)/([a-z]+)/$ /subdomains/%1/domap.php?sub=%1&map1=$1&map2=$2 [L]

#### SOME PROBLEM HERE IF PEOPLE JUST REQUESTING [sub.domain.nl...]

# index (SUB add slash)
# RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
# RewriteRule ^$ [%1.domain.nl...] [R=301]

# index (SUB rewrite)
RewriteCond %{HTTP_HOST}!^www\.domain\.nl
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
RewriteRule ^$ /subdomains/%1/ [L]

jdMorgan

4:06 pm on Aug 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



#### SOME PROBLEM HERE IF PEOPLE JUST REQUESTING [sub.domain.nl...]

# index (SUB add slash)
# RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
# RewriteRule ^$ http://%1.domain.nl/ [R=301]


That's because if they request "/" then you redirect them to "/", and that creates a loop.

If your intent is to redirect a request for "http://sub.domain.nl" with no trailing slash to "http://sub.domain.nl/" with a trailing slash, then you need to look at {REQUEST_URI} and make sure it has no trailing slash before redirecting:


RewriteCond %{REQUEST_URI} !/$
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.nl
RewriteRule ^$ http://%1.domain.nl/ [R=301]

Note that this is only needed in .htaccess; In httpd.conf, the leading slash is "visible" to RewriteRule.

Jim

jd01

5:09 pm on Aug 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you could make a speed adjustment in this set by taking advantage of a S (skip) and only checking the HOST once:

# if the HOST is not www.domain.nl skip the next 6 rules
RewriteCond %{HTTP_HOST} !^www\.domain\.nl
RewriteRule . - [S=6]

# add /
RewriteCond %{REQUEST_URI}!/$
RewriteRule ^$ http://%1.domain.nl/ [R=301]

# 0..9 (WWW rewrite)
RewriteRule ^([0-9]+)/$ /do.php?page=$1 [L]

# map1 (WWW add slash)
RewriteRule ^([a-z]+)$ http://www.domain.nl/$1/ [R=301]

# map1 (WWW rewrite)
RewriteRule ^([a-z]+)/$ /domap.php?map1=$1 [L]

# map2 (WWW add slash)
RewriteRule ^([a-z]+)/([a-z]+)$ http://www.domain.nl/$1/$2/ [R=301]

# map2 (WWW rewrite)
RewriteRule ^([a-z]+)/([a-z]+)/$ /domap.php?map1=$1&map2=$2 [L]

Justin