3

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

<VirtualHost 109.73.70.169:80>
    <IfModule mod_userdir.c>
        UserDir disabled
        UserDir enabled USERNAME
    </IfModule>
    <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>
    <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>
        <IfModule mod_php5.c>
            php_admin_value open_basedir "/home/USERNAME/:/usr/lib/php:/usr/local/lib/php:/tmp"
        </IfModule>
        <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"
        </IfModule>
    </IfModule>
    ServerName DOMAIN.net
    ServerAlias *.DOMAIN.net
<Directory "/home/USERNAME/public_html">
    Options FollowSymLinks
    Allow from all
    AllowOverride All
</Directory>
    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>
        suPHP_UserGroup USERNAME USERNAME
    </IfModule>
    <IfModule !mod_disable_suexec.c>
        SuexecUserGroup USERNAME USERNAME
    </IfModule>
    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"

</VirtualHost>

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

Eray
  • 139
  • 2
  • 12
  • 1
    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 02 '12 at 03:48
  • `AllowOverride not allowed here`: says what the problem is. Totally unrelated to the rewrite. That user is using a directive in .htaccess that isn't allowed (think about it, if you'd allow [AllowOverride](http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride) in .htacces, then it has zero use for server and vhost level and the webserver admin looses control). –  Jan 22 '14 at 23:50

2 Answers2

0

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.

fsniper
  • 121
  • 3
  • 1
    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
0

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]