Solved

htaccess rewrite for subdomain

Posted on 2004-04-26
20
2,429 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:bertstevens
  • 11
  • 8
20 Comments
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10924838
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.
0
 

Author Comment

by:bertstevens
ID: 10926133
tnx, i''ll try

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

what if it can contain almost all text/numbers?
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10926185
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.
0
 

Author Comment

by:bertstevens
ID: 10926189
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


0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10926230
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 ".".
0
 

Author Comment

by:bertstevens
ID: 10926281
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
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10926404
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]
0
 

Author Comment

by:bertstevens
ID: 10930598
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}
0
 

Author Comment

by:bertstevens
ID: 10930623

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}
0
 

Author Comment

by:bertstevens
ID: 10930958
'In this case, yes, user.domain.com  has to be declared in DNS.'

What should i ask my sys admin then?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 10

Expert Comment

by:Mercantilum
ID: 10931044
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.
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10931098
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).
0
 
LVL 10

Accepted Solution

by:
Mercantilum earned 200 total points
ID: 10931371
This should work

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

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

Author Comment

by:bertstevens
ID: 10931594
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?
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10934631
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]
0
 

Author Comment

by:bertstevens
ID: 10936428
nope that doesn't work
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10936491
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.
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10936540
>> 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 :)
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10936545
Sorry I missed the R !!

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

Expert Comment

by:periwinkle
ID: 11761550
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
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now