Link to home
Start Free TrialLog in
Avatar of bertstevens
bertstevens

asked on

htaccess rewrite for subdomain

I'm looking for a full
.htaccess file todo this:

forward   username.site.com  to
www.site.com/show.php?user=username

and www.site.com/username should go to www.site.com/show.php?user=username


What permissions do i need to set on the .htaccess file (chmod) ?

Do i need to ask the server admin to change dns settings or so?
Avatar of Mercantilum
Mercantilum
Flag of Japan image

1. access rights

Ensure the web server can read the file.
Ensure only you or root can write to the file.
If the file belongs to you, just do

   chmod 644 .htaccess

6 = 1 0 1 = r w -  (owner of the file)
4 = 1 0 0 = r -  -  (group of the file)
4 = 1 0 0 = r -  -  (others)

You can do this as well:  

   chmod u=rw,a=r .htaccess


2. mod_rewrite

- About DNS: DNS resolve the host name, i.e. in your case "www.site.com", ie everything between http:// and the first following /.-
So since after the rewrite the domain doesn't change you shouldn't have to modify DNS.

Rewrite: ensure you have these lines in your .htaccess


- Rules to rewrite your url

RewriteEngine On
RewriteRule  ^/show\.php  -  [L]
RewriteRule  ^/([a-z]+)$   http://www.site.com/show.php?user=$1  [L,R]

Assuming a user name is made of a to z characters.
Avatar of bertstevens
bertstevens

ASKER

tnx, i''ll try

'Assuming a user name is made of a to z characters'

what if it can contain almost all text/numbers?
Just change the [a-z] with what you like:

RewriteRule  ^/([a-z0-9]+)$   http://www.site.com/show.php?user=$1  [L,R]

for a-z plus 0-9 : user21321 or 123user is ok.
Hi

i placed your code exactly in htaccess but it has no effect. Also if i rename the file to .htaccess i can't
see it anymore with flashfpx


Are you sure your .htaccess is read?
Have a look to http://httpd.apache.org/docs/howto/htaccess.html

You can have the same behaviour in your httpd.conf file if you put the mod_rewrite directives above between <Directory ...> directives.

E.g.
<Directory /path/where/mod_rewrite/tobedone>
RewriteEngine On
RewriteRule  ^/show\.php  -  [L]
RewriteRule  ^/([a-z]+)$   http://www.site.com/show.php?user=$1  [L,R]
</Directory>

The directory is likely to be where you .htaccess file was.

On Unix/Linux do ls -la to see the files starting with a ".".
httpd.conf <- i don't think i have access to that...

when a type user.domain.com  nothing happens, and on domain.com/user i get a no permission error
If you don't manage the server, it is possible that the admin did not load and start the mod_rewrite module.
In this case the RewriteRule above would be ignored...

By the way in your initial question you never mention " user.domain.com ".
In this case, yes, user.domain.com  has to be declared in DNS.

The rules would be
RewriteEngine On
RewriteRule  ^/show\.php  -  [L]
RewriteRule  ^user.domain.com   http://www.site.com/show.php?user=user  [L,R]
None of the code posted above works :( :( :(
Increasing points for someone who does know the sollution to this apperently simple common problem...

RewriteEngine On
RewriteRule  ^/showuser\.php  -  [L]
RewriteRule  ^/([a-z0-9]+)$   http://www.domain.nl/showuser.php?user_login=$1  [L,R]

this is the only code that at least opens the currect url (but with wrong parameter)

RewriteCond %{HTTP_HOST} !^www\.domain\.nl
RewriteRule ^(.*)$  http://www.domain.nl/showuser.php?users_name=%{HTTP_HOST}

i want to forward  user123.domain.nl to www.domainnl/showser.php?user_login=user123 and also
www.domain.nl/user123 to www.domain.nl/showser.php?user_login=user123

the only code that came close to working is :

RewriteCond %{HTTP_HOST} !^www\.domain\.nl
RewriteRule ^(.*)$  http://www.domain.nl/showuser.php?users_name=%{HTTP_HOST}
'In this case, yes, user.domain.com  has to be declared in DNS.'

What should i ask my sys admin then?
For instance if you domain is  domain.com, and www refers to the address of your web site inside this domain, you want (probably) to get the same address for both   user123.domain.com  and  www.domain.com  

So if the DNS map of your domain domain.com has the following host

www                IN  A         1.2.3.4

Ask your admin either to add

user123            IN  CNAME   www

or

user123            IN  A        1.2.3.4

and restart "named"


Regarding the mod_rewrite, I cannot test the rules I gave you, for the time being. Will have a re-look later.
By the way in case you don't know yet... to get an idea of how apache will expand and process your rules, add

    RewriteLog "/var/log/httpd/rewrite.log"
    RewriteLogLevel 2

to get a log of mod_rewrite (2 is not that verbose).
ASKER CERTIFIED SOLUTION
Avatar of Mercantilum
Mercantilum
Flag of Japan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
solved with:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.nl
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.nl  [NC]
RewriteRule ^(.*)$ http://www.domain.nl/showuser.php?user_login=%1 [L]

RewriteCond %{HTTP_HOST}   ^www\.domain\.nl
RewriteCond %{REQUEST_URI} ^/([^.]+)$ [NC]
RewriteRule ^(.*)$ http://www.domain.nl/showuser.php?user_login=%1 [L]


i would say 200 points to merca... maybe the code can be made a bit more clean/fast?
2nd case (www), the default being the URI in the modification rule, you don't need the request_uri line
(note that the rewrite rule line changed)

RewriteCond  %{HTTP_HOST}   ^www\.domain\.nl
RewriteRule  ^/([^.]+)$  /showuser.php?user_login=$1 [L]
nope that doesn't work
Since it works on my server, exactly as written above, let see what could be the possible problems

1. Is /showuser.php at the root? Isn't it in /cgi-bin/showuser.php ?
   in this case the rule should be RewriteRule  ^/([^.]+)$  /cgi-bin/showuser.php?user_login=$1 [L]

2. The rule you initially wrote (2nd case) is for an url like
   http://www.domain.nl/user123
   http://www.domain.nl/showuser.php?user_login=user123
Is it really what you want to do.

Please have a look to
1.  rewrite.log
2.  your error log: it will show what exactly it was trying to reach on your server after all rewritings.
>> In the case of 1...  (cgi-bin)
- you need a redirect, as /cgi-bin/ and documents are not in the same directory tree
- if you use redirect, you need to prevent a rewrite looping, so I add the first of the 2 rules below
   (could be done with a RewriteCond as well)

So, in this case (showuser in cgi-bin) rules can be

   RewriteRule  ^/cgi-bin  -  [L]
   RewriteRule  ^/([^.]+)$  /cgi-bin/showuser.php?user_login=$1 [L]

That should be it :)
Sorry I missed the R !!

 RewriteRule  ^/cgi-bin  -  [L]
 RewriteRule  ^/([^.]+)$  /cgi-bin/showuser.php?user_login=$1 [L,R]
Avatar of periwinkle
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:

    ACCEPT: Mercantilum

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Periwinkle
EE Cleanup Volunteer