2011-01-04 4 views
0

J'essaie de créer des sous-domaines via htaccess. Le code ci-dessous ne veut exactement que je veuxrediriger vers plusieurs sous-domaines virtuels en utilisant htaccess

Il faut http://domain.com et redirect à http://www.domain.com

Options -Indexes 
DirectoryIndex index.html index.htm index.asp index.php 
ErrorDocument 401 http://www.domain.com 
ErrorDocument 403 http://www.domain.com 
ErrorDocument 404 http://www.domain.com 
ErrorDocument 500 http://www.domain.com 
ErrorDocument 507 http://www.domain.com 
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^domain.com$ 
RewriteRule ^(.*) http://www.domain.com/$1 [QSA,L,R=301] 
AddType text/html .html .htm .asp 

Ceci est la partie que je ne suis pas à Sûre de:

RewriteCond %{HTTP_HOST} ^domain.com/nl$ 
RewriteRule ^(.*) http://nl.domain.com/$1 [QSA,L,R=301] 

Comment puis-je créer virtuelle sous-domaines de sorte que si quelqu'un va à http://nl.domain.com il resterait sur http://nl.domain.com si quelqu'un tape http://www.nl.domain.com il faudrait sortir le http://www.nl.domain.com et le rendre http://nl.domain.com également la structure de répertoire pour le sous-domaine serait http://www.domain.com/nl (C'est là que les fichiers réels seront assis). Donc si quelqu'un va à http://www.domain.com/nl, il devrait également rediriger vers http://nl.domain.com.

Merci à l'avance pour des conseils et des pointeurs

Répondre

2
RewriteEngine on 

# The ordering of the following rules is somewhat important 

# 
# External redirects with HTTP "301 - Moved Permanently" for subdomains 
# 

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

# Instead I could do this to redirect any prefix before nl to nl.example.com 
# RewriteCond %{HTTP_HOST} ^.+?\.nl\.example\.com$ 
# RewriteRule ^(.*) http://nl.example.com/$1 [QSA,L,R=301] 

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

# Instead I could do this to redirect any prefix before foo to foo.example.com 
# RewriteCond %{HTTP_HOST} ^.+?\.foo\.example\.com$ 
# RewriteRule ^(.*) http://foo.example.com/$1 [QSA,L,R=301] 

# Rewrite any remaining subdomains to example.com 
RewriteCond %{HTTP_HOST} !^example\.com$ 
RewriteCond %{HTTP_HOST} !^nl\.example\.com$ 
RewriteCond %{HTTP_HOST} !^foo\.example\.com$ 
RewriteRule ^(.*) http://example.com/$1 [QSA,L,R=301] 

# Assuming from this point forward we have either 
# example.com, nl.example.com, or foo.example.com as the HTTP_HOST 

# Redirect example.com/nl to nl.example.com 
# (Note that ONLY example.com/nl is caught here.) 
RewriteCond %{HTTP_HOST} ^example\.com$ 
RewriteRule ^nl(/(.*))? http://nl.example.com/$2 [QSA,L,R=301] 

# Redirect example.com/foo to foo.example.com 
# (Note that ONLY example.com/foo is caught here.) 
RewriteCond %{HTTP_HOST} ^example\.com$ 
RewriteRule ^foo(/(.*))? http://foo.example.com/$2 [QSA,L,R=301] 

# 
# Internal rewrites for directory structuring 
# 

# Internal rewrite for the nl subdomain 
# - Match the subdomain exactly 
RewriteCond %{HTTP_HOST} ^nl\.example\.com$ 
# - Check to see if the rewrite already happened (prevent 
# infinite loop of internal rewrites) 
RewriteCond %{REQUEST_URI} !^/nl(/.*|$) 
# - Rewrite the URL to the subdirectory 
RewriteRule ^(.*) /nl/$1 [L] 

# Internal rewrite for the foo subdomain 
# - Match the subdomain exactly 
RewriteCond %{HTTP_HOST} ^foo\.example\.com$ 
# - Check to see if the rewrite already happened (prevent 
# infinite loop of internal rewrites) 
RewriteCond %{REQUEST_URI} !^/foo(/.*|$) 
# - Rewrite the URL to the subdirectory 
RewriteRule ^(.*) /foo/$1 [L] 

Je ne l'ai pas testé ci-dessus sur un serveur, mais je l'ai testé sur mon serveur local, il devrait être proche de ce que vous avez besoin si Je vous ai bien compris. Je suis sûr que vous avez vu le mod_rewrite docs. En plus de cela, le Rewrite Guide et le Advanced Rewrite Guide ont des exemples pratiques utiles.

+0

Salut Robert merci beaucoup pour l'effort de me fournir une solution au problème. Je vais le tester un peu plus tard aujourd'hui et je vous ferai savoir si cela a fonctionné! –

Questions connexes