mod_rewrite based on user name

Discussion in 'Apache' started by amiprog, May 7, 2013.

  1. #1
    I am using apache with subversion and need to redirect a request for a file to a different file based on the user that has made the request. It appears that the URL for the requested file could be changed on the fly using RewriteMap; however, is there any way to get the User (as typically given in the request_rec available to apache hooks) making the request in the program used?
    Thanks
     
    amiprog, May 7, 2013 IP
  2. Markwebuk

    Markwebuk Well-Known Member

    Messages:
    1,595
    Likes Received:
    17
    Best Answers:
    2
    Trophy Points:
    113
    #2
    Though this isn't the best solution, but based on your query, let me try and help you achieve it.
    First-of-all, you must build profile.php file in a way that is capable to accept the username.

    Once this is done, you'd need to modify the .htaccess by adding a rule similar to the one below :
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
     
    RewriteRule ^([a-zA-Z0-9]+)/$ profile.php?username=$1
    Code (markup):
    In this you'd find a trailing slash which can allow you to use another rule which adds a trailing slash to every request which is made (which avoids duplicate content having different pages one with and one withouth the slash).

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(.*)/$ # check if the URL already ends with a slash
    RewriteRule ^(.*)$ http://website.com/$1/ [L,R=301]
    Code (markup):
    I hope this is something that you were looking for. If not, then please elaborate on the situation that you have.
     
    Markwebuk, May 8, 2013 IP
  3. amiprog

    amiprog Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Hi Markwebuk,
    Thanks for the information. I'm not sure if this will achieve my goal or not. Where does the "username" come from in this solution? Is it being provided by the user when the request is sent to the server or is it provided as part of the HTTP header itself? Typically, when a HTTP request is sent to the apache server for subversion, the username is passed in the HTTP header and Apache adds it to the request_rec structure. But in your suggested code, I don't understand how the username is provided. Can you explain this?
    Thanks!
     
    amiprog, May 8, 2013 IP