I need some help with htaccess. I have this to make nice urls for usernames. RewriteRule ^([0-9a-zA-Z\-]+)$ ./profil.php?user=$1 Code (markup): To show something like: site.com/user Now I want this to be transformed into user.site.com but also to keep this one. So I want both of them to work. How to achieve this ? Thanks!
From the top of my head: RewriteRule ^http://(.*).site.com/(.*)$ http://site.com/profil.php?user=$1$2 Code (markup):
Agreed, but it'll break other pages, no? Like site.com/help/about would redirect you to profile.php?user=help/about ?
depends, www.site.com/ breaks, http://site.com does not. www.site.com/help/about would be rewritten to site.com/wwwhelp/about in this case
Then it should be split into two rules: ^http://(.*).site.com/$ http://site.com/p.php?user=$1 and ^http://(.*).site.com/(.*)/$ http://site.com/p.php?user=$2 Hope that $2 in the second regex isn't greedy, if it isn't it should fix /help/about-like issues, because of the trailing slash there it should only pickup arguments without slashes.
I found this code: RewriteCond %{HTTP_HOST} !^www\.uf1\.info RewriteCond %{HTTP_HOST} ([^.]+)\.uf1\.info [NC] RewriteCond %{REQUEST_URI} !^/profil\.php$ RewriteCond %{QUERY_STRING} !^user=. RewriteRule (.*) /profil.php?user=%1 [QSA,L] Code (markup): But it is not working for me... it is correct ? Also I have tried your version fabiodan but is not working.
It's not a good idea to copy apache .htaccess files, they depend too much on things we're all to young to know... First off, remove all the RewriteCond lines for now, just to test. Then make the profil.php just print out the user parameter. Carefully observe what it prints out, then experiment. There's no single-fits-all solution in this case, I'm afraid. Are you developing on a local machine or on a server+domain already?
Try this # taken care of author.site.com # no rewrite on www.site.com, site.com, with /profil.php and user= RewriteCond %{HTTP_HOST} !^www\.uf1\.info RewriteCond %{HTTP_HOST} ^([0-9a-zA-Z\-]+)\.uf1\.info RewriteCond %{REQUEST_URI} !^/profil\.php$ RewriteCond %{QUERY_STRING} !^user=. RewriteRule ^http://([0-9a-zA-Z\-]+)\.uf1\.info$ /profil.php?user=$1 # taken care of site.com/author # only for www.site.com # no rewrite on profil.php and user= RewriteCond %{HTTP_HOST} ^www\.uf1\.info RewriteCond %{REQUEST_URI} !^/profil\.php$ RewriteCond %{QUERY_STRING} !^user=. RewriteRule ^([0-9a-zA-Z\-]+)$ /profil.php?user=$1 Code (markup): See if that works for you. - Rufas
@Rufas, not working, works only the second code with site.com/author, but the first one with author.site.com doesn't works. @fabiodan, I am testing on a live server, not on localhost.
Have you set the wildcard subdomains yet? You have to add this line to your httpd.conf: <VirtualHost 10.1.2.3:80> ServerName example.com [b]ServerAlias *.example.com example.com[/b] DocumentRoot /www/htdocs/site_example </VirtualHost> Code (markup): The fact is subdomain can use different path than main domain. This line above will tell the server the location of your files and allow the subdomain to be resolved correctly. Only then your server can see your htaccess file (and the code we suggested above) to do all the redirecting. On CPanel, you don't have to edit httpd.conf file. You can just add a subdomain like you normally do, except this time it is named * (as in *.site.com). Make sure you set the correct path. After that you can try the code again. See if that works for you this time. - Rufas
Thank you it worked, I tested it on another server, the one I tested it first time doesn't have httpd.confd and does not allow me to add * subdomain. It is free hosting from 000webhost.