URL rewriting - PHP & MySQL: Novice to Ninja

Hi, Tom.

Thank you VERY much for getting back to me.

FIRST POINT


I have removed ALL files from www.framework.local, the virtual host AND I’ve removed all files from var/www/html (the default apache content folder).

I already had the .htaccess file but I have copied this into both directories AND I’ve created an index.php with the code you suggested (plus a bit to differentiate the two folders… you’ll see) and saved that into each folder.

In Chrome:

  1. URL “www.framework.local” outputs: “At /var/www/www.framework.local, you are viewing /”

  2. URL “www.framework.local/foo/bar” outputs: “Not Found
    The requested URL /foo/bar was not found on this server.
    Apache/2.4.25 (Debian) Server at www.framework.local Port 80”

  3. URL “localhost” produces: “At /var/www/html, you are viewing /”

  4. URL “localhost/foo/bar” produces: “Not Found
    The requested URL /foo/bar was not found on this server.
    Apache/2.4.25 (Debian) Server at localhost Port 80”

  5. Running “tail -n 200 /var/log/apache2/error.log” after this shows data but nothing to do with the Page Not Founds. Virtually nothing in there to be honest.

I think we CAN say from this that the .htaccess file is NOT redirecting File Not Founds to index.php. But why?

POINT TWO


I have PHP 7 installed on my dev machine so there’s nothing stopping the code from running. I am just targetting the 5.6 methods/syntax where necessary as my IDE only supports 5.6. Thus far, that has only been the lack of the Null Coalescing Operator. All other code thus far has been run through a version checker I use and is proven to run perfectly in 5.6 anyway. So, it’s not that that is causing problems.

With respect, I understand that you can’t cater for every possible setup but I think it’s a bit unfair to say: “I can only guarantee the code will work if you use Windows with VirtualBox and Vagrant and the Homestead VM.”

This is seriously specific and, honestly, if it had said that on the book cover I would never have bought it.

People have spent £25 or more on your book, have devoted weeks of time to following through it. Not everyone has or wants your very specific setup.

My set up is not bizarre. Just standard Debian 9 packaged versions of Apache2, PHP7 and MariaDB10. And this is exactly what is running on the production server I pay for. ISP’s wouldn’t touch an environment like yours as they are reputed to be insecure: Homestead, XAMPP, no. They run environments like mine.

So I do not accept that a book which says it teaches PHP and MySQL should provide code which does not run on a bog standard Debian 9 installation.

I think you have a responsibility, while as I say not covering every setup possible, to cover basic ones like a straight Debian/Ubuntu install or a straight Fedora install. Just an appendix which says: "If you choose to work in a more “regular” environment such as Debian’s or Fedora’s certified stable packages (an environment which more closely matches what you’ll find on a production server), then you need to make the following few alterations to your setup for the code in the book to work. Don’t you?

Any further help you can offer me will be greatly appreciated so that I can benefit from the generic framework the book promises.

Thanks in advance.

Mike :slight_smile:

Hey, Tom.

Progress… thanks to your /foo/bar suggestion.

In Debian systems, there is a file, “/etc/apache2/apache2.conf”. In this, you’ll find the following block:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted

Changing None to All gets me to a point where using the URL www.framework.local/foo/bar in Chrome now produces the output: “At /var/www/www.framework.local, you are viewing /foo/bar”. Hallelujah. Praise the Lord! A nice piece of plaster board has appeared in my stud partition and the plasterers are on their way around. :slight_smile:

I already had rewriting enabled before I started these posts but, if it helps others, this is achieved in Debian with the command: a2enmod rewrite.

OK. Now I’m going back to the code and trying again.

I am going to continue posts to this thread IF I come across anything else that doesn’t work in Debian Standard install and any fixes I have for this. Hope this helps others.

And, thanks, Tom. Sometimes finding something requires someone to tell you exactly where you should be looking and you did that today. Cheers.

An aside, not particularly for Tom…

I may be about to make an idiot of myself but, for years I have wondered, when I came across these terms, Why “foo” and “bar”? What that about?

???

I have given step by step instructions for getting the system up and running on Apache. The instructions I have provided work on an Apache server with mod_rewrite enabled and configured to accept htaccess files. If it’s not working you will need to try to work out what’s going wrong with your server configuration. Do you have AllowOverride set correctly? Is Apache configured to use .htaccess as the configuration filename? These are things that are well beyond the scope of the book and something you will need to look into for your specific setup.

As you have suggested, URL rewriting is not working at all with your configuration. Whether it’s because the .htaccess file is being ignored or for some other reason there is no way I can provide instructions for every single set up/potential configuration difference.

To prove that the .htaccess file works, download a preconfigured LAMP server and try it for yourself:

vagrant init scotch/box
vagrant up

Write

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

into `.htaccess` 

then 


```php
<?php
echo 'You are viewing ' . $_SERVER['REQUEST_URI'];
```

into index.php and visit `http://192.168.33.10` and `http://192.168.33.10/foo/bar` both work as expected. 

You might wish  to download scotch/box and check to see where the configuration changes from yours.

Never mind, I see that you got it working while I was replying.

They’re generic placeholder terms widely used in programming. A quick google search will give a far more comprehensive answer to their history: https://softwareengineering.stackexchange.com/questions/69788/what-is-the-history-of-the-use-of-foo-and-bar-in-source-code-examples

As a final point, even if you don’t want to use a VM for development, I would generally recommend NGINX over Apache. Apache works fine but is generally much more difficult to configure for no benefit. Unless you have a specific need for something Apache offers that NGINX does not, NGINX is generally faster and easier to configure.

Tom,
incase of another time, please be helping us provide a through guide for apache configuration because majority of our ISP uses apache that is why most of us in my group uses xampp.

Since a VPS costs as little as $3 a month, there’s no reason to use shared hosting any more. Get a VPS and set up the server however you like it. argumentum ad populum has never been a good reason to choose a technology. With that said, even if you do want to use that as a reason to choose apache, Apache’s market share is in decline while NGINX is on an upward trend: https://news.netcraft.com/archives/2018/04/26/april-2018-web-server-survey.html in a few years Apache will not be installed on most ISPs and you’ll have to learn NGINX, why not be ahead of the game?

Setting up a server is a big topic (regardless of whether it’s NGINX or Apache) and not covered in the book because it would make the book a third as large again on top of the 600+ pages it already contains.

If you’re using XAMPP you’re getting someone else to configure the server for you anyway, you might as well use a pre-configured VM. The pros/cons of XAMPP vs VM are outlined in the book and here: https://www.sitepoint.com/re-introducing-vagrant-right-way-start-php/

Tom ,
thanks for this qreat info, I too will soon switch to NGINX

That’s interesting. I was completely unaware of that. Thanks for the heads up, Tom. I’ll certainly look at that.

One seemingly contradictory point I will make is: If it ain’t broke, don’t fix it.

If you already have a website up and running using Apache, there is little benefit to switching it to NGINX. In fact, due to subtle differences between the servers, you may even find some things don’t quite work correctly after the switch (though it’s unlikely, it may happen). If, however, you are starting a new project today, or doing a complete rebuild of an existing site, then NGINX is usually the better choice.

If it’s just development machine, then go ahead and switch, you’re not going to break a real website that people are trying to use!

Tom,

The .htaccess file… the last line… [quote=“TomB, post:24, topic:290945”]
RewriteRule ^.*$ index.php [NC,L,QSA]
[/quote]

If we follow your advice of putting index.php inside a “public” directory, how does this line still work? Should it not be:
RewriteRule ^.*$ public/index.php [NC,L,QSA]

Mike

No, because the .htaccess file and index.php are both in the same directory (public). public/index.php would look for an index.php at public/public/index.php.

Tom,

Cheers. I think that’s all the pieces in place.

Mike

Tom.

I have returned the code to my virtual host and… success! Thank you very much for your help.

Mike

p.s. I totally agree with you that you shouldn’t have to provide detailed instructions on configuring all manner of setups but I would consider the two main ones (for the 7th edition): Debian and Red Hat. One of these two is what will be running on a production server, I would guess. And, as we’ve seen with Debian, only two or three commands needed to get me past a section in the book which I’ve been stuck on for weeks. Give it some thought. :slight_smile:

A post was split to a new topic: PHP & MySQL novice to ninja - what’s next?

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