Need help with htaccess

Discussion in 'Programming' started by cipcip, Aug 14, 2011.

  1. #1
    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!
     
    Solved! View solution.
    cipcip, Aug 14, 2011 IP
  2. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #2
    From the top of my head:
    RewriteRule ^http://(.*).site.com/(.*)$ http://site.com/profil.php?user=$1$2
    Code (markup):
     
    ssmm987, Aug 15, 2011 IP
  3. fabiodan

    fabiodan Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Agreed, but it'll break other pages, no? Like site.com/help/about would redirect you to profile.php?user=help/about ?
     
    fabiodan, Aug 15, 2011 IP
  4. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #4
    ssmm987, Aug 15, 2011 IP
  5. fabiodan

    fabiodan Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    fabiodan, Aug 15, 2011 IP
  6. cipcip

    cipcip Well-Known Member

    Messages:
    1,935
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    160
    #6
    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.
     
    cipcip, Aug 15, 2011 IP
  7. fabiodan

    fabiodan Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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?
     
    fabiodan, Aug 15, 2011 IP
  8. Rufas

    Rufas Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    0
    #8
    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, Aug 15, 2011 IP
  9. cipcip

    cipcip Well-Known Member

    Messages:
    1,935
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    160
    #9
    @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.
     
    cipcip, Aug 15, 2011 IP
  10. #10
    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
     
    Rufas, Aug 16, 2011 IP
  11. cipcip

    cipcip Well-Known Member

    Messages:
    1,935
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    160
    #11
    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.
     
    cipcip, Aug 16, 2011 IP