PHP Novice to Ninja 6 -- URL Rewriting using XAMPP

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?

I’d be very grateful for any insights or advice.

What do you have in your .htaccess file?

Thanks, Gandalf458. Here’s what I have (I copied this code from PHP Novice to Ninja 6):

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [NC,L,QSA]

I tried this, an empty htaccess file, and a couple of other statements. Seemed to make no difference.

I’ve moved this to the Server config forum which seems more appropriate.

Just a thought, does it work if you remove the slash before index.php in the last line?

Hi Matt,

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:

  1. 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.

  2. 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)

1 Like

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.

Hi Tom,

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!

Thanks again.

I have not read all the book, but shouldn’t index.php be in the root directory too?
@TomB can maybe confirm.

Thanks, I’m glad you’re enjoying the book!

If you are using xampp you will need to place any files I put in the public directory in your htdocs directory, including index.php

1 Like

That did the trick! Fantastic. So happy I can move on to the next step. Thank you thank you!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.