views:

57

answers:

2

Almost in any project I work on, some issues with .htaccess occur. I usually just find the easiest solution and leave it because I don't have any knowledge or understanding for Apache, servers etc. But this time I thought I would ask you guys.

This is the files and folders in my (simplified) setup:

/modrewrite-test
    .htaccess
    /config
    /inc
    /lib
    /public_html
        .htaccess
        /cms
            /navigation
                index.php
                edit.php
            /pages
                index.php
                edit.php
        login.php
        page.php

The "config", "inc" and "lib" folders are meant to be "hidden" from the root of the website. I try to accomplish this by making a .htaccess-file in the root that redirects the user to "public_html". The .htacess-file contains this:

RewriteEngine On
RewriteRule   (.*) public_html/$1

This works perfect. If I type "http://localhost/modrewrite-test/login.php" in my browser, I end up in public_html/login.php which is my intention. So this works fine. The .htaccess-file in "public_html" contains this:

RewriteEngine On

# Root
RewriteRule   ^$ page.php [L]

# Login
RewriteRule   ^(admin)|(login)\/?$ login.php [L]

# Page (if not a file/directory)
RewriteCond   %{REQUEST_FILENAME} !-d
RewriteCond   %{REQUEST_FILENAME} !-f
RewriteRule   ^(.*)$ page.php?url=$1 [L]

The first rewrite just redirects me to public_html/page.php if I try to reach "http://localhost/modrewrite-test/". The next rewrite is just for the convenience of users trying to log in - so if they try to reach "http://localhost/modrewrite-test/admin" or "http://localhost/modrewrite-test/login" they will end up at the login.php-file. The third and last rewrite handles the rest of the requests. If I try to reach "http://localhost/modrewrite-test/bla/bla/bla" it will just redirect me to public_html/page.php (with the 'url' GET-variable set) instead of finding a folder called "la", containing a folder named "bla" and etc.

All of these things work perfect but a minor issues occurs when I for instance try to reach "http://localhost/modrewrite-test/cms/navigation" without a slash at the end of the URL. When I try to reach that page the browser is somehow redirected to "http://localhost/modrewrite-test/public_html/cms/navigation/". The correct page is shown but why does it get redirected and add the "public_html" part in the URL? The desired behavior is that the URL stays intact and that the page public_html/cms/navigation/index.php is shown.

The files and folders in the (simplified) can be found at http://highbars.com/modrewrite-test.zip

A: 

RewriteBase may help. Try this in public_html/.htaccess:

RewriteEngine On
RewriteBase /
jholster
Nothing new happens. It doesn't change the issue I'm highlighting. Another thing is that I shouldn't even be able to reach http://localhost/modrewrite-test/public_html/* unless I have a folder named "public_html" in the "public_html"-folder (which I do not have in my simplified setup).
Orhan Toy
A: 
MicE
This "RewriteBase"-rule doesn't seem to do anything different. I've tried your suggestion and Yaggo's but the result is the same when the rule is applied and also when it's not applied.My exact (simplified) setup is at http://highbars.com/modrewrite-test.zip if you want to try it for yourselves.
Orhan Toy
I did download the original piece which you linked in your question when I was testing this yesterday. I just re-did the whole exercise and can confirm that there's still a problem - it however manifests only if there is no slash at the end of URLs. Looking into it...
MicE
@Orhan: please see my answer above (I edited it). I don't know how to do what you would like to achieve, so I suggested an alternate solution.
MicE
It's funny how I keep on getting back to some kind of a MVC pattern when I'm trying to do it my own way :) But yeah, your suggestion with a Front Controller is something I've tried with a MVC (kind-of) structure but I just feel that a MVC structure is too much for simple applications, plus the freedom I get from my own home-made structure is really nice.Would it be possible to just point the root to public_html? That's basically what the outer .htacces does. I've looked at Redirect and Alias but I don't think that's the way to go.
Orhan Toy
I agree, mod_rewrite is the better way in this case. As you said, you are already pointing everything to `public_html` by means of the main `.htaccess` file. I think that you won't have the problem if you just keep static files (css/img/js) and the Front Controller in `public_html`. You don't have to stick specifically to MVC even if you are using the Front Controller pattern - you can just use it to bootstrap your other UIs.
MicE