2

I tried to make a .htacces to simulate dynamics subdomains. like his post : .htaccess: mod-rewrite; subdomain

but it's impossible for me to get the expected result.

i have read several times the doc at : apache

i have no problem with : RewriteCond, RewriteRule, regular expression. But i have problems with the subdomain.

i have this page : tododiversion.es and for my example, I want this :

chr.tododiversion.es ==> tododiversion.es/chr/

i put .htaccess in the www folder: www/ .htaccess

and the folder "chr" is in : www/chr/

in the htacces i put :

RewriteEngine On

# host starts with something else  
RewriteCond %{HTTP_HOST} ^([^\.]+)\.tododiversion\.es$  [NC]

# rewrite 
RewriteRule ^(.*)$ /chr  [L]

it doesn't work..

I know that urlrewritting is working because I made a simple test. I know that RewriteCond is working because I made a simple test. I was trying on my computer with localhost and 127.0.01 and it didn't work neither.

If someone could give me some advice it should be great! Chris

Community
  • 1
  • 1
chris44
  • 33
  • 5

2 Answers2

0

The issue could be that apache is not properly configured to handle subdomains. Make sure that you have all subdomains set up to forward to this directory, or the .htaccess will never be executed.

<VirtualHost *:80>
ServerName domain.com
ServerAlias *.domain.com
DocumentRoot /var/www/domain.com
</VirtualHost>

Remember to restart Apache after updating your config files.

For a reference, see How to configure subdomains for Apache2 on Ubuntu?

Community
  • 1
  • 1
mattc
  • 485
  • 3
  • 9
  • 1
    I thought it was not mandatory to create a subdomain. Like in the referenced question that i have added. For example: if the subdomain is "username".hostname. and there is always new usernames. I will study your link tomorrow. Thanks – chris44 Oct 23 '12 at 22:05
  • I believe you need to configure Apache to properly handle the subdomains, but I could be wrong. I've updated my answer with a config file example. – mattc Oct 24 '12 at 06:31
  • i tried it but it didn't work like i want. in the post I mentioned previously it sound the solution is working! i really don't understand... – chris44 Oct 24 '12 at 20:19
  • in apache documentation : You must first have your DNS server properly configured to map those names to an IP address associated with your server. – chris44 Oct 24 '12 at 20:19
  • We'll assume that subdomain configuration isn't the problem. I just noticed a bug in the rewrite rule, so I'm going to post another solution for you. This probably isn't the best way to do this on Stack Overflow but I'm new here. – mattc Oct 24 '12 at 21:04
0

You need to include $1 in the rewrite rule so that any URL specifics after the domain are passed on.

chr.tododiversion.es/page.html ==> tododiversion.es/chr/page.html

Based on the link you provided, the rewrite rule should be as follows:

# rewrite
RewriteRule ^(.*)$ /%1/$1  [L]
mattc
  • 485
  • 3
  • 9
  • thank you for your help. It was a problem of DNS.. I should use DNS wildcard. I read that it was possible without special DNS configuration... However I think people saying that it's possible withtout special DNS configuration have the right configuration, but they don't know it – chris44 Oct 27 '12 at 07:56
  • more detail there : http://stackoverflow.com/questions/4194655/wildcard-in-dns-hosts-file – chris44 Oct 27 '12 at 08:04
  • That makes sense. Glad you were able to figure it out. If you could, write up an answer here on how you were able to fix the problem so you can mark this problem as closed. – mattc Oct 27 '12 at 12:54