(.htaccess) how to mod_rewrite a tilde user site to subfolder within same site

Discussion in 'Apache' started by kdwoell, Aug 23, 2012.

  1. #1
    I have been working this issue for several nights with no luck. I'm trying to Rewrite web requests using (.htaccess)
    from: cobweb.seas.gwu.edu/~mpnl
    to: cobweb.seas.gwu.edu/~mpnl/joomla

    My latest (.htaccess) file is below:

    
    # turn on Rewrite
    RewriteEngine on
    
    
    RewriteCond %{HTTP_HOST} ^(www.)?cobweb.seas.gwu.edu/~mpnl$
    RewriteCond %{REQUEST_URI} !^/joomla/
    
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    
    RewriteRule ^(.*)$ /joomla/$1 [L]
    
    
    RewriteRule ^(/)?$ joomla/index.php [L]
    
    Code (markup):
    This seems a trivial task and yet the solution eludes me.
     
    kdwoell, Aug 23, 2012 IP
  2. shindig

    shindig Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Your HTTP_HOST is wrong. It should only be the host name sent in the HTTP header, in this case cobweb.seas.gwu.edu. You'll thus need to also correct the REQUEST_URI, etc
     
    shindig, Sep 2, 2012 IP
  3. kdwoell

    kdwoell Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Hmm. I tried the suggestion, and changed HTTP_HOST to cobweb.seas.gwu.edu (removing the ~mpnl), and updating _URI and other variables accordingly but I still get the 500 Internal Server Error.

    -------------code starts here-----------------

    # .htaccess main domain to subfolder redirect
    # turn on Rewrite
    RewriteEngine on


    RewriteCond %{HTTP_HOST} ^(www.)?cobweb.seas.gwu.edu$ [NC]


    RewriteCond %{REQUEST_URI} !^cobweb.seas.gwu.edu/joomla/


    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d


    RewriteRule ^(.*)$ cobweb.seas.gwu.edu/joomla/$1 [L]


    RewriteRule ^(/)?$ cobweb.seas.gwu.edu/joomla/index.php [L]
     
    kdwoell, Sep 2, 2012 IP
  4. shindig

    shindig Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    REQUEST_URI only takes the URI. Thus, if you only wanted to check for '/~mpnl/' then it'd be

    RewriteCond %{REQUEST_URI} ^/~mpnl/$
    Code (markup):
    Apologies, I should have been clearer with my first reply.
     
    shindig, Sep 2, 2012 IP
  5. kdwoell

    kdwoell Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    There are two REQUEST_URI statements in the proposed code, and I tried various combinations of your new statement i.e. both, only the first, only the second and each case, returns 500 Internal Error. Last attempted (.htaccess) file pasted below.
    Did I mention Apache mod_userdir is enabled? Don't know if that has any bearing.

    
    RewriteEngine on
    
    
    RewriteCond %{HTTP_HOST} ^(www.)?cobweb.seas.gwu.edu$ [NC]
    
    
    RewriteCond %{REQUEST_URI} ^/~mpnl/$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    
    RewriteRule ^(.*)$ joomla/$1 [L]
    
    
    RewriteCond %{REQUEST_URI} !^/~mpnl/joomla/
    RewriteRule ^(/)?$ joomla/index.php [L]
    
    Code (markup):
     
    kdwoell, Sep 3, 2012 IP
  6. shindig

    shindig Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hmmm, it shouldn't. Rewrites get dealt with at an earlier stage.

    
    RewriteRule ^(.*)$ joomla/$1 [L]
    
    Code (markup):
    That's incorrect. The RewriteRule URL should be the full one, ie

    
    RewriteRule ^(.*) http://cobweb.seas.gwu.edu/~mpnl/joomla/ [L]
    
    Code (markup):
    Note that if you add '$1' onto the end, the final URL you'll end up on is http://cobweb.seas.gwu.edu/~mpnl/joomla/~mpnl/ :)

    
    RewriteCond %{REQUEST_URI} !^/~mpnl/joomla/
    RewriteRule ^(/)?$ joomla/index.php [L]
    
    Code (markup):
    I am not sure what you are trying to do there. Are you wanting anything outside of '/~mpnl/joomla/' (and by extension, '/~mpnl/' as you have the redirect above it) to be rewritten to in the background to 'joomla/index.php' to make the URLs SEO-friendly?
     
    Last edited: Sep 3, 2012
    shindig, Sep 3, 2012 IP
  7. kdwoell

    kdwoell Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    Nothing that complicated. I only learned about (.htaccess) files a couple of weeks ago. If a user enters cobweb.seas.gwu.edu/~mpnl the browser should redirect to cobweb.seas.gwu.edu/~mpnl/joomla/index.php. I suppose going to the Joomla root would be sufficient. The web server account has everything in: public_html/
    In the root of public_html is the old site. In public_html is a folder /joomla.
    I'm converting to a Joomla-based website.

    I'm officially confused now. Can someone provide the complete (.htaccess) file? Not just bits and pieces.
     
    kdwoell, Sep 3, 2012 IP
  8. shindig

    shindig Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It can take a while to get used to how it works, it's a real swiss-army knife because you can put (most) Apache directives in them and that means that it can get complicated. Even the experts can get caught out by it sometimes, so don't worry.

    '~' is a Unix shorthand for the home directory so ~mpnl translates to /home/mpnl. From your post above it sounds like the joomla stuff is located in /home/mpnl/public_html/joomla. Thus, what you'll need in the Virtual Host, main server config or the conf file for mod_userdir is the directive:

    
    Userdir public_html 
    
    Code (markup):
    Obviously, this needs to appear *after* the mod_userdir module is loaded :)

    Now, by doing this, a request to http://cobweb.seas.gwu.edu/~mpnl/joomla/ will cause Apache to look for the files in /home/mpnl/public_html/joomla.

    
    RewriteEngine on
    #You may need to escape the . in (www.) but you shouldn't have to (I tested this on an Ubuntu server and it worked fine)
    RewriteCond %{HTTP_HOST} ^(www.)?cobweb\.seas\.gwu\.edu$ [NC]
    RewriteCond %{REQUEST_URI} ^/~mpnl/$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*) http://cobweb.seas.gwu.edu/~mpnl/joomla/ [L]
    
    Code (markup):
    The above will look at the supplied http host and request URI and if it matches it'll redirect to http://cobweb.seas.gwu.edu/~mpnl/joomla/. You don't need to redirect/rewrite anything to index.php as it should be one of the default index files and so will be used automatically.
     
    shindig, Sep 3, 2012 IP
  9. kdwoell

    kdwoell Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #9
    Hmm. Still getting the 500 Internal error.

    Unclear what you mean by escape the "." in (www.)

    If it helps, I do know these facts:
    virtual server: i.e. sitting down with the web admin one day he mentioned a virtual config file
    OS: Solaris
    PHP: v4.3.7 (yeah I know its ancient but they refuse to upgrade due to legacy PHP code by other users)
    PHP mode: PHP is installed as an Apache module, not as cgi. I know this since I had to get the admin to chmod the entire Joomla folder to user:group (www:www). I could not install any Joomla extensions via the admin backend until this was done. Plus I can run a php info script and confirm such.
    mod_userdir: enabled
    mod_rewrite: enabled
     
    kdwoell, Sep 4, 2012 IP
  10. shindig

    shindig Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hmm, when I tested the .htaccess instructions I gave above on a Centos and Ubuntu machine they worked fine. What is the actual error log in the logs for that Virtual Host? Apache on Solaris may work differently than Linux. Hell, the problem may be that the virtual host doesn't have permissions to overwrite certain Apache directives via .htaccess. What Apache version is it?

    With regex ('regular expressions') certain characters have special meanings depending on the type of regex being used (Perl, Python, etc). For example, '$' means 'end of the line' and '^' means 'start of the line' and a '.' means 'any single character excluding a newline'. Escaping one of these characters tells the regex parser to treat that character as a regular one and you do this by preceding it with '\'. Thus, '\$' and '\.' will be seen as a regular '$' and '.'

    Apache uses Perl's regex implementation (more specifically, Perl 5).
     
    shindig, Sep 4, 2012 IP
  11. kdwoell

    kdwoell Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #11
    I see. What should the code look like with the "." escaped? Like this?

    [COLOR=#3E3E3E]RewriteEngine on[/COLOR]
    #You may need to escape the . in (www.) but you shouldn't have to (I tested this on an Ubuntu server and it worked fine)RewriteCond %{HTTP_HOST} ^(www'\'. )?cobweb\.seas\.gwu\.edu$ [NC]RewriteCond %{REQUEST_URI} ^/~mpnl/$RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d [COLOR=#3E3E3E]RewriteRule ^(.*) http://cobweb.seas.gwu.edu/~mpnl/joomla/ [L][/COLOR]
    Code (markup):

    Regarding the web logs. I don't have access to the server, and the administrator has another full-time job, i.e. web administration is a collateral duty, so I rarely get responses. I'm kinda on my own.

    I do know for a fact AllowOveride was set to "All" at one point while the admin and I were fixing a PHP problem but he might have turned that off. Should I test for that somehow?
     
    kdwoell, Sep 5, 2012 IP
  12. shindig

    shindig Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    No, you don't need to put the apostrophes in there :) It'd look like this:

    
    RewriteCond %{HTTP_HOST} ^(www\.)?cobweb\.seas\.gwu\.edu$ [NC]
    
    Code (markup):
    Ah man :( I hate it when that happens. This means we're shooting in the dark here :(


    If it was turned off then it won't read the .htaccess file at all, so you wouldn't be getting the error messages. Could you please paste the entire contents of the .htaccess file for me?
     
    shindig, Sep 5, 2012 IP
  13. kdwoell

    kdwoell Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #13
    The latest (.htaccess) file was exact copy n paste from your earlier entry.

     [COLOR=#111111]RewriteEngine on[/COLOR]
    #You may need to escape the . in (www.) but you shouldn't have to (I tested this on an Ubuntu server and it worked fine)RewriteCond %{HTTP_HOST} ^(www.)?cobweb\.seas\.gwu\.edu$ [NC]RewriteCond %{REQUEST_URI} ^/~mpnl/$RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d [COLOR=#111111]RewriteRule ^(.*) http://cobweb.seas.gwu.edu/~mpnl/joomla/ [L][/COLOR] 
    Code (markup):
    and here is the version where I tried "." escaped.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?cobweb\.seas\.gwu\.edu$ [NC]
    RewriteCond %{REQUEST_URI} ^/~mpnl/$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*) http://cobweb.seas.gwu.edu/~mpnl/joomla/ [L]
    
    Code (markup):
    Both return a 500 internal server error.


     
    kdwoell, Sep 5, 2012 IP
  14. shindig

    shindig Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Hmmmmmm, please try and get the server admin to get the error message from the logs, I'm grasping at straws here. This works fine on the Linux boxes I tested on

    Try the following:
    
    
    <IfModule mod_rewrite.c>
    RewriteEngine on RewriteCond %{HTTP_HOST} ^(www\.)?cobweb\.seas\.gwu\.edu$ [NC] RewriteCond %{REQUEST_URI} ^/~mpnl/$ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*) http://cobweb.seas.gwu.edu/~mpnl/joomla/ [L]
    </IfModule>
    
    
    
    Code (markup):
    The 'IfModule' parts will only kick into effect if mod_rewrite is enabled. If that still throws an error, change:

    
    
    RewriteCond %{REQUEST_URI} ^/~mpnl/$
    
    Code (markup):
    to

    
    RewriteCond %{REQUEST_URI} ^/non-existent-url/$
    
    Code (markup):
    as well as

    
    
    RewriteRule ^(.*) http://cobweb.seas.gwu.edu/~mpnl/joomla/ [L]
    
    Code (markup):
    to

    
    RewriteRule ^(.*) http://cobweb.seas.gwu.edu/non-existent-url/joomla/ [L]
    
    Code (markup):
    It does not matter if that URI doesn't exist, you'll simply get a 404 when the rewrite kicks in. I'm wanting to check if there isn't something weird with Solaris and using home directories.
     
    shindig, Sep 5, 2012 IP
  15. kdwoell

    kdwoell Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #15
    I sent an email to the web admin this morning requesting read access only to the Apache error logs. I would be shocked if I even get a reply but will stay on it.

    Your (.htaccess) file, in each case, throws the same 500 Internal Error.

    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?cobweb\.seas\.gwu\.edu$ [NC]
    RewriteCond %{REQUEST_URI} ^/~mpnl/$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*) http://cobweb.seas.gwu.edu/~mpnl/joomla/ [L]
    </IfModule>
    
    Code (markup):
    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?cobweb\.seas\.gwu\.edu$ [NC]
    RewriteCond %{REQUEST_URI} ^/~mpnl/$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*) http://cobweb.seas.gwu.edu/~mpnl/joomla/ [L]
    </IfModule>
    
    Code (markup):
    The last version did not throw a 404, but the same 500 Internal Error.
    <IfModule mod_rewrite.c>RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?cobweb\.seas\.gwu\.edu$ [NC]
    RewriteCond %{REQUEST_URI} ^/non-existent-url/$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*) http://cobweb.seas.gwu.edu/non-existent-url/joomla/ [L]
    </IfModule>
    Code (markup):
     
    kdwoell, Sep 6, 2012 IP
  16. shindig

    shindig Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Hmmmmmmm.... let us try bypass the damn .htaccess file altogether until this is sorted. If you can run PHP files in the home directory, create an index.php file that does a 302 redirect or similar to the new directory. Otherwise, use static html file with a meta refresh that does the redirection. This will bind you over till you get a response from the server admin.
     
    shindig, Sep 6, 2012 IP