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
Solved

htaccess rewrite for subdomain

Posted on 2004-04-26
20
2,434 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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