Forum Moderators: phranque
I posted here a while ago, because I was having problems getting wildcard dns and mod_rewrite to play nicely together. It turned out that putting my code in the main httpd.conf did the trick.
I was able to make the subdomains work, mapping *.mydomain to user/user.php?user=*, but when I try to change the code so more than one page can be accessed, my error log says:
RewriteRule: bad flag delimiters
This is when using the code found in [webmasterworld.com...]
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.example\.com(:80)?<>/([^/]*) [NC]
RewriteCond %1<>%3!^(.*)<>\1$ [NC]
RewriteRule ^(.*) /%1/$1 [L]
however if i replace the last line with
RewriteRule ^(.*) user/user.php?user=%1 [L]
it works fine.
Is there something I'm doing wrong or missing? My goal is to have something like:
RewriteRule ^(.*) user/<filename>.php?user=%1 [L]
~Creaturecorp
That fur-ball of code you copied is much more complex than what you need if you're putting the code in httpd.conf. The added complexity is only needed to work in the .htaccess enviroment, preventing an "infinite" loop for internal requests which have already been rewritten to the subdomain-subdirectory. You don't need that complexity in httpd.conf.
In addition, the code will only work on a server running an OS that supports POSIX 1003.2 regular expressions -- a minority. This is because is uses POSIX 1003.2 "atomic back-references" in the fourth line of code to do a "compare" of the requested subdomain and the requested top-level subdirectory. If they are already the same, then the rewrite is not performed, preventing a loop.
For httpd.conf, all you should need is something like this:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule (.*) /%1/$1 [L]
An example of case-conversion using RewriteMap and the system tolower command is given in the mod_rewrite documentation. That and other reference documents are cited in our forum charter.
Also, note that I can criticize the code you found because I wrote it. :)
Jim
I have a virtualhost setup so my domain responds to subdomain requests.
<VirtualHost 69.9.xx.#*$!>
DocumentRoot /home/xxx/public_html/
ServerAlias *.example.com
ServerName www.example.com
</VirtualHost>
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule (.*) /%1/$1 [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule [b]^/([/b].*) /%1/$1 [L]
Jim
In light of this problem, perhaps it would be better to keep it in htaccess. I'm not truly doing subdomains: but rather something like this:
RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule ^journal/(.*) user/journal.php?user=%1 [L]
RewriteRule ^gallery/(.*) user/gallery.php?user=%1 [L]
RewriteRule ^activity/(.*) user/activity.php?user=%1 [L]
RewriteRule ^(.*) user/user.php?user=%1 [L]
The ^<inser_page_name_here>/(.*) doesn't seem to work. Any suggestions?
Could something like this be done instead? I think it will be better to have specific rewrites to keep things secure.
Thanks for your help and patience.
One thing that I should point out is that a RewriteCond applies only to the first (single) RewriteRule that follows it. Therefore, %1 is undefined for all but your first rule. You could use user-defined envars to overcome this, though.
Do you have FollowSymLinks enabled?
How about RewriteEngine on?
I have to ask, because you don't show them.
Jim
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule .* - [E=userID:%1]
RewriteRule ^(journal¦gallery¦activity)/ /user/$1.php?user=%{ENV:userID} [L]
RewriteRule .* /user/user.php?user=%{ENV:userID} [L]
The variable name "userID" is arbitrary, except that it must not conflict with any system-defined variable.
Replace the broken pipe "¦" characters above with solid pipes from your keyboard; Posting on this board modifies them.
Jim
For some reason that line is giving me trouble.
Also, I've got another pile of rewrites in the file. Because of this, something like the following may occur:
http://example.com/view/1
and
[username.example.com...]
will bring up the same url. The latter url shouldn't bring up anything.
#################
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule .* - [E=userID:%1]
RewriteRule ^(journal¦gallery¦activity)/ /user/$1.php?user=%{ENV:userID} [L]
#RewriteRule ^ /user/user.php?user=%{ENV:userID} [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)[/]?$ /$1.php [L]
RewriteRule ^fav/([0-9]+)[/]?$ /fav.php?id=$1 [L]
RewriteRule ^view/([0-9]+)[/]?$ /view.php?id=$1 [L]
RewriteRule ^full/([0-9]+)[/]?$ /view.php?id=$1&full=1 [L]
RewriteRule ^comments/([0-9]+)[/]?$ /reply.php?id=$1 [L]
#################
Thanks so much for your help, It's truly appreciated.
RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule .* - [E=userID:%1]
#
RewriteCond %{ENV:userID} .
RewriteRule ^(journal¦gallery¦activity)/ /user/$1.php?user=%{ENV:userID} [L]
#
RewriteCond %{ENV:userID} .
RewriteRule .* /user/user.php?user=%{ENV:userID} [L]
If you end up with more that just a few rules based on userID, then you could use a [S]kip construct to eliminate multiple checks of the userID for non-blank.
Jim
RewriteRule .* user/user.php?user=%{ENV:userID} [L]
(the slash before user was causing an error)
In my post above, you saw how I have some conditions that follow the subdomain user rewrite. Would it be possible to change
RewriteCond %{REQUEST_FILENAME} !-d
to cancel all the following rewrites if the %{ENV:userID} is set? This would mean that nothing except the subdomain rewrite would execute, therefore preventing any conflicts. (eg http://example.com/view/1/ being accessible at [username.example.com...] shouldn't occur)
Your help has really been great, just what the doctor ordered. :)
RewriteCond %{ENV:userID} .
RewriteRule .* - [L]
I'll refer you to the documentation now, to analyze the code line-by line so that you can understand it and modify it to suit your changing needs over time. Several good links are posted in our charter, and there are also some good threads in the WebmasterWorld library [webmasterworld.com].
Jim