I’ve been working along with the Jokes Database project in PHP Novice to Ninja 6, and I’ve hit a snag involving URL Rewriting. The authors use the Homestead Improved box, but I’m using XAMPP (on a Mac), and I wonder if that’s the root of the problem. Basically, my URLs aren’t getting rewritten as they should, so none of the routes work. (If I type the whole URL into the IJDBRoutes->callAction() method, it works fine – so I’m pretty sure the rest of the code is okay.)
Also, mod_rewrite is working in Apache. I’ve tested it using very simple files.
Here’s my question: Is there something about XAMPP that works differently from the authors’ set-up, and means I’ll have to proceed in a different way? Or can I just tweak .htaccess or something to get the site working as it should?
The code you have there from the book is correct and will work with Apache as long as mod_rewrite is enabled. All requests should be forwarded to index.php. If you visit a page that does not exist on the server e.g. http://localhost/not-found/ you should see the same as http://localhost
If that’s not happening, here’s a few things to check:
Your file is named .htaccess including the dot prefix and isn’t called .htaccess.txt or similar. Windows and MacOS can be a bit of a pain when dealing with files that start with a dot.
The .htaccess file is stored at the root of the public_html (or public, htdocs or httpdocs) directory (depending on how your server is configured)
Thanks for moving the question; I’m new to Sitepoint, so not too familiar with the organization of things. … I tried removing the slash. Doesn’t seem to make a difference. I wonder what I’m doing wrong! Could be something basic.
Thanks for your reply. I’m guessing you may be one of the book’s authors? Let me say, it’s a great book: I’ve learned a ton from it, so I’m very grateful! But to the point: yes, .htaccess has the dot prefix. And it’s stored at the root level (for me, that’s htdocs).
index.php is in a top-level folder called “public.” That’s not a problem, is it?
I ran the test of mod_rewrite that’s described in Learn Apache mod_rewrite, and it worked as it should.
I think there may be something more basic that I don’t understand. If I echo $route from index.php, I get “public/index.php”. If that gets passed into the construction of EntryPoint, how does $route end up equalling joke/list or some such? Shouldn’t the names of the controller and method be passed into it?
The code all works, but only if I stick a statement like “else if ($route === ‘public/index.php’)” into IjdbRoutes! And that’s not how it’s supposed to work!