.htaccess headaches

smalpierre

New member
Ok, so I've been using the Slim MVC framework as a front controller for a long time. It basically has an .htaccess file so when the url "http://www.mysite.com/routeidentifier/querystringdata" is called, it redirects to /index.php and looks for a route to the controller, and calls the controller specified.

Now I'm building my own front controller to eliminate a dependency for one, and also because I want to store routes in the database instead of in index.php so when I have thousands of links, I don't have a huge file to process or look at.

Anyway - what I did, was grabbed the .htaccess file from the root directory, and copied it to a new site's base directory. Then I created an index.php file. It can be anything - all it has to contain for the purpose of this test is something like var_dump($_SERVER['REQUEST_URI']); to keep it simple.

On my laptop (wampserver on Windows) it works as I expect it to: It should take every request no matter what, and route it to index.php in the document root which right now just displays everything you typed after the TLD part of the url. Well technically it displays data type, data, and length since I'm var_dumping instead of echoing ...

So if you type http://mylaptop.com it will return:
string '/' (length=1)

If you type http://mylaptop.com/index.php it returns the same thing

If you type http://mylaptop.com/directory/that/doesnt/exist it will return:
string '/directory/that/doesnt/exist' (length=28)

Now - I have a single folder under document root called "folderexists" with an index.php file that contains nothing but "echo("This folder actually exists!");"

If you type http://mylaptop.com/folderexists it will return:
This folder actually exists!

So this works exactly as I would like it to ... Now to the problem!

On my linux box, (Debian 8, Apache running php-fpm and opcode caches) it doesn't work so well.

if you type http://myserver.com it will return:
string '/' (length=1)

If you type http://myserver.com/index.php it returns the same thing

If you type http://myserver.com/directory/that/doesnt/exist it will return:
404 File Not Found (with apache notification underneath)

I haven't tried it with the directory that exists yet ...

If on the other hand you type http://myserver.com/index.php/directory/or/file/that/doesnt/exist it will return
string '/index.php/directory/or/file/that/doesnt/exist' (length=46)

That's because the file actually exists - index.php - so it doesn't have to redirect.

So the redirect obviously isn't working right?

Then why if I type http://myserver.com/doesntexist.php it returns
file not found

Which is different than the Apache
404 File Not Found (with apache notification under it)


My question is how do I make my server respond like my laptop? Here's the .htaccess contents:

Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

It should work, and on the exact server it's not workin on in my simple case, it works for Slim MVC???
 

smalpierre

New member
Ok, I feel like a TOTAL idiot!

I figured out what was wrong .. my virtualhost configuration had an "Allowoverride none", so the .htaccess was being ignored.

Fixed that, then played with it for a while. So far this is what I've got:

Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /

# I'll be using this later, to redirect any requests to mydomain.com to www.mydomain.com
# It should keep the end of the URL intact - so mydomain.com/somewhere/file.php would redirect to
# www.mydomain.com/somewhere/file.php
#rewritecond %{http_host} ^somedomain.com [nc]
#rewriterule ^(.*)$ http://www.somedomain.com/$1 [r=301,nc]

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

RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]
 
I don't know what to say. I only use .htaccess to block IP's. I use custom error pages for files not found and put blank index.html files in folders I don't want people to look at. Less confusing and easier to set up. No need to rewriting a .htaccess file.

Although I have one for my CMS, but found it does the samething with or without it.
 

ogah

New member
that is all in one permalink htaccess.
with that htaccess you can make many permalink without setting each permalink in htaccess rule, only need play with index.php to handle it
 

smalpierre

New member
That's basically what I'm doing. I'm building a front controller in PHP. If the resource exists - like a javascript file, or image - it will directly access it. If it doesn't, it will match the url to a slug in the database, and call the controller specified and pass it any data fields from the url.

So it'll work like this - your slug might look like /users/edit/:userid

where :userid is a data field. The request would look like http://www.yoursite.com/users/edit/joe

So it'll set a variable named userid to a value of joe, then the controller can use that to do whatever, and process a view.

pseudocode for index.php would be something like this:

urlSlug = getUrlSlug(urlString);

ctrlSlug = getCtrlSlug(urlSlug)

if (urlSlug == false){
err = 404
} else {
process(ctrlSlug);
}