URL Rewriting - PHP & MYSQL: NOVICE TO NINJA

I have all downloaded [CMS-Controller-Rewrite] sample files from the github (https://github.com/spbooks/phpmysql6/tree/CMS-Controller-Rewrite) and I have Apache server. So, I created a file called [.htaccess] in the new [public_html] directory with the following contents:

conf

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^.*$ /index.php [NC,L,QSA]

(all instructions from the book “PHP & MYSQL: NOVICE TO NINJA” page 375). Doing these steps, the project does not work anymore - all links are not correct.

I presume that my URL rewriting is wrong.

Could anybody explain how to do URL rewriting correctly, thanks.

I am unfamiliar with that first directive. What does “conf” do?

I have no idea. It is from the book (p.375)

I have a feeling it is a typo. Try temporarily removing that line from the htaccess file and see if it helps any.

1 Like

Not a typo in the book. I don’t know how or where you got “conf” but I don’t see it

Removing “conf” do not help. My other question is:

I have all downloaded [CMS-Controller-Rewrite] sample files from the github (https://github.com/spbooks/phpmysql6/tree/CMS-Controller-Rewrite) and I have Apache [XAMPP] server. I presume that the code of all these files is correct. But it doesn’t work neither with .htaccess file (code from the book) neither without it.

What should I do to make the project working?

I don’t see the .htaccess file on github, I would expect it to be in the public folder, maybe @TomB did not include the .htaccess
I just tried the code exactly as in the book and it appears to work as expected (using WAMP).

Check that the file is named correctly, some editors will try and add an extension.
Also I sometime find when editing .htaccess that it sometimes caches so I don’t see changes I made. I have spend many times of frustration, doubting my abilities in htaccess, when all along it was still using an older version of the file. :banghead:

Does it work with or without .htaccess?

Hi dmitrijs63,

Can you elaborate on “not working”? Do you get any error messages?

I am redirecting to wrong links (home, list, edit), accordingly I have error 404 “Object not found!”

I tested the code on a local version of a site I manage. So strictly speaking, it’s not the exact same as in the book, because there was already other stuff in the htaccess. I just pasted the code in near the top.
But it certainly does what it is supposed to.
Without the code, a nonexistent page gives a 404. With the code, it redirects to the homepage.

So I need .htaccess. Could anybody provide the code for it (for XAMP)? In which directory it must be located?

If you are getting 404 it means the .htaccess file is probably not being recognized. Make sure it’s in your public_html (or whatever you have your public directory configured as) directory l, it’s correct named, the server is correctly configured with mod rewrite enabled and allows htaccess files.

Could you explain step by step where I should to create the “public_html” directory and how to configure it as public?

It will depend on how you have Apache configured. You should place the htaccess file where you place your index.php

Everything in public from the sample code will also need to be placed in this directory.

I have root directory (http://localhost/phpmysql6-CMS-Controller-Rewrite) in which all sample folders (classes, includes, public, templates) and files are located. index.php is located in public directory so I’ve put .htaccess file in public directory. The code of .htaccess is:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^.*$ /index.php [NC,L,QSA]

When I open

http://localhost/phpmysql6-CMS-Controller-Rewrite/public/index.php

the link is replacing by

http://localhost/phpmysql6-CMS-Controller-Rewrite/public/phpmysql6-cms-controller-rewrite/public/index.php

and I have error 404
I have same result when .htaccess is located in root directory.
Are you sure that the code of .htaccess is ok?

You will need to move everything up two levels so that the website is on http://localhost/ and everything that was in the public directory is in the website’s root directory, then the rest of the files are not public.

My website’s folder name is “phpmysql6-CMS-Controller-Rewrite”, so it is on http://localhost/ already. I described it in previous post. Are you sure that this code of .htaccess is ok?

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^.*$ /index.php [NC,L,QSA]

You have it in a subdirectory of localhost, although you could put your htaccess file in the public directory your routes will not work as is.

I have all downloaded “CMS-Controller-Rewrite” sample files from the github (https://github.com/spbooks/phpmysql6/tree/CMS-Controller-Rewrite) and this .htaccess file:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^.*$ /index.php [NC,L,QSA]

Could anybody explain one more time what should I do with all this stuff to make the project working on XAMPP?