?
Solved

htaccess rewrite for subdomain

Posted on 2004-04-26
20
Medium Priority
?
2,440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 
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 600 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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

741 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