Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm trying to rewrite my URL's subdomain.

http://username.domain.com will show content of http://www.domain.com/user.php?u=username but URL stay as http://username.domain.com . (I mean url masking)

(Username's can contain a-z 0-9 and hypens) Also if subdomain is www or api, don't redirect them

I'm using this for my .htaccess

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule .* /user.php?u=%1 [L]

(After @mfarver's advice) I'm trying this

RewriteEngine on
RewriteRule .* /user.php?u=%1 [L]

but this time getting 500 Internal Server error:

[Mon May 30 20:10:44 2011] [alert] [client 81.6.xx.xxx] /home/blablabla/public_html/.htaccess: AllowOverride not allowed here

(from error log)

My server's httpd.conf file's virtualhost settings

    <IfModule mod_userdir.c>
        UserDir disabled
        UserDir enabled USERNAME
    <IfModule concurrent_php.c>
        php4_admin_value open_basedir "/home/USERNAME/:/usr/lib/php:/usr/php4/lib/php:/usr/local/lib/php:/usr/local/php4/lib/php:/tmp"
        php5_admin_value open_basedir "/home/USERNAME/:/usr/lib/php:/usr/local/lib/php:/tmp"
    <IfModule !concurrent_php.c>
        <IfModule mod_php4.c>
            php_admin_value open_basedir "/home/USERNAME/:/usr/lib/php:/usr/php4/lib/php:/usr/local/lib/php:/usr/local/php4/lib/php:/tmp"
        <IfModule mod_php5.c>
            php_admin_value open_basedir "/home/USERNAME/:/usr/lib/php:/usr/local/lib/php:/tmp"
        <IfModule sapi_apache2.c>
            php_admin_value open_basedir "/home/USERNAME/:/usr/lib/php:/usr/php4/lib/php:/usr/local/lib/php:/usr/local/php4/lib/php:/tmp"
    ServerName DOMAIN.net
    ServerAlias *.DOMAIN.net
<Directory "/home/USERNAME/public_html">
    Options FollowSymLinks
    Allow from all
    AllowOverride All
    DocumentRoot /home/USERNAME/public_html
    ServerAdmin webmaster@DOMAIN.net
    UseCanonicalName Off
    CustomLog /usr/local/apache/domlogs/DOMAIN.net combined
    CustomLog /usr/local/apache/domlogs/DOMAIN.net-bytes_log "%{%s}t %I .\n%{%s}t %O ."
    ## User USERNAME # Needed for Cpanel::ApacheConf
    <IfModule mod_suphp.c>
    <IfModule !mod_disable_suexec.c>
        SuexecUserGroup USERNAME USERNAME
    ScriptAlias /cgi-bin/ /home/USERNAME/public_html/cgi-bin/

    # To customize this VirtualHost use an include file at the following location
    # Include "/usr/local/apache/conf/userdata/std/2/USERNAME/DOMAIN.net/*.conf"


Also *.DOMAIN.net added to my DNS ZONES as A record.

share|improve this question

marked as duplicate by Jenny D, mdpc, HBruijn, Hyppy, Dave M Dec 9 '14 at 19:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Looks good but debugging mod_rewrite is always tricky. I would recommend breaking it down. Comment out all of the conditions and make sure the rule works first, then add them back one at a time. In my experience mod_rewrite behaves slightly different in .htaccess vs in the conf files. Also make sure that you are actually using .htaccess (ie overrides are allowed) –  mfarver May 30 '11 at 18:05
I'm sure server using .htaccess file. Because when i type random characters to my .htaccess file, i'm getting 500 INTERNET SERVER error. So my .htaccess file is working. I'm not similar with .htaccess commands very well. First i will comment out 2nd 3th and 4th lines right? –  Eray May 30 '11 at 18:30
@mfarver, after your advice i'm delete conditions but this time i'm getting error. Please look ADDITION 1 on answer. I was edited it. –  Eray May 30 '11 at 18:35
Question improved totally –  Eray May 31 '11 at 18:12
You probably forgot to restart httpd after editing httpd.conf –  Alvin Wong Sep 2 '12 at 3:48

2 Answers 2

If you are using a .htaccess file, your server should have AllowOverride direction for these domain contexts. Check you servers general configuration.

Also you need a redirect for thise kind of rewriting.

instead of: RewriteRule .* /user.php?u=%1 [L]

use: RewriteRule .* http://www.domain.com/user.php?u=%1 [r,L]

After your edit: you do not need redirecting.

share|improve this answer
After chatting over this problem on another platform I have concluded this problem is not only about rewriting. Domain names and vhost configuration are not set properly. –  fsniper May 30 '11 at 20:50

This should do the job - you need "PT" to stop it from being a redirect. If it isn't a redirect the URL won't change in the browser:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule .* /user.php?u=%1 [PT,L]
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.