1

So I just have begun to build a website from the groundup. I have never did this before. I'm working on a server.

1. Question:

Within my file index.php I have the following content:

<?php include("html/Start.html"); ?>

It works great, the content of Start.html is displayed. Within the folder html I have more files, like courses.html for example. Within this file I wanted to connect to my database (works great in Start.html) but I then noticed I couldn't use any php code inside courses.html. Including a html file didn't work inside courses.html, for example.

What did I do wrong?

2. Question:

I was thinking about a smart solution for the website and their subpages. When clicking on a link at the navigationbar like "Courses" I don't want mywebsite.com/html/courses.html to be displayed, more like something mywebsite.com/offers/courses

but how do I do that?

Excuse me for these basic questions, but I have no idea where to start. If there is something unclear, feel free to leave a comment, thank you

Phil
  • 437
  • 2
  • 6
  • 25

3 Answers3

2

1.- Answer:

Just change the .html to .php, remember, your server detects an HTML file not a PHP file, so the PHP is not parsed and is detected as "plain text" (or part of the HTML), so only use <?php include("html/Start.php"); ?> and then the PHP code inside your Start.php file will be executed.

2.- Answer:

That is called "SEO" and "Friendly URLs" you can achieve that in Apache with a file called .htaccess.

Check this out for example:

https://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049

Community
  • 1
  • 1
Asfo
  • 409
  • 8
  • 18
0

1st. Code is not working in courses.html file because it should be courses.php (.php extension). As php compiler only run code through the .php files. It was working with start.html because you included that file in index.php (".php") that is a php file and it ran the code in start.html. Replace extensions to .php

2nd. You can write rules for your .htaccess file to wanted url scheme. (use google to find toturial)

anees
  • 1,272
  • 1
  • 11
  • 22
0

You can edit your server configuration so that PHP will process .html files as well as .php files. For Apache you can edit the AddHandler directive to

AddHandler application/x-httpd-php .php .html

Note that if you do that, all .html files will be processed by the PHP interpreter, even those that do not include any PHP code.

The simplest way to get the URLs you are wanting is to actually set up your directory structure in that way. i.e. create a /offers/courses directory in your web root and add an index.php there (or index.html, if you have already modified your web server configuration) with the content of courses.html. Then the mywebsite.com/offers/courses will display that index file. You may need to modify your server configuration again for this to work. For Apache, set the DirectoryIndex directive to

DirectoryIndex index.html index.php

Be sure to restart your web server after making any configuration changes in order for them to take effect.

Or instead, if you are using more of an MVC type setup, where a front controller processes the URL and directs you to different routes based on the URL segments, there's a lot of good information on that here.

Don't Panic
  • 37,589
  • 9
  • 55
  • 71