How to create dynamic web pages like WordPress does?

We are building a PHP site with CMS, and would like to give the users the ability to dynamically create new pages just like WordPress does.

All the pages will have the same format - but we just need to know if it's possible to define htaccess in a way that any site address always resolves to our single template. The idea is that no matter what address the user typed in their browser, it would resolve to our template page and our template page could determine which page was being requested and read the database for that content.

1) User enters in http://mydomain.com/contactus in their browser
2) Our htaccess file redirects the request to http://mydomain.com/index.php
3) Our index.php file reads the address (http://mydomain.com/contactus) and then fetches content from the database with the key: contactus and displays the page.

Is this a reasonable approach? If so, how could the htaccess file be configured?

If not, is there a reasonably straightforward way to accomplish this?

Many thanks experts!
LVL 1
rascalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
This is exactly how most of the CMS's do it.

The .htaccess basically has a condition to see if the file specified in the URL actually exists.

If it does not it then parses the URL and sends the results off to index.php adding parameters to the back of the script.

index.php then examines the parameters and internally loads the content (from DB or through an include / require statement)

Here is a sample .htaccess
AddType x-mapp-php5 .php
Options +FollowSymLinks

RewriteEngine on
RewriteBase /

# condition: file must not exist
RewriteCond %{REQUEST_FILENAME} !-f
# send it off to index.php
RewriteRule (.*).html$ index.php?page=$1 [L,NC,QSA]

Open in new window

In  the last line above the rule matches anything with .html extension. It removes the filename and adds this as the page parameter to the call to index.php. The QSA directive says append any additional parameters that were in the url to this as well.

So contact.html?fullname=bob&surname=smith&email=bob@smith.com
Becomes
index.php?page=contact&fullname=bob&surname=smith&email=bob@smith.com
0
rascalAuthor Commented:
Thanks julian,
This gets me nearly there, and I understand what you're saying, but the addresses first coming in would not be anything with an extension such as .html etc., all the adresses coming into the site would simple be http://www.mydomain.com/contactus with no extension.

Would the model above still apply then?  Would it just be:

# condition: file must not exist
RewriteCond %{REQUEST_FILENAME} !-f
# send it off to index.php
RewriteRule (.*).$ index.php?page=$1 [L,NC,QSA]

Open in new window

0
rascalAuthor Commented:
Here is what WordPress uses:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Open in new window


So I think if I just use that I should be good to go?
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

Julian HansenCommented:
Yes - there are many ways to skin the cat. I posted what we use - which works for our model but adapt to yours as you see fit. The principle is sound and in regular use though - which is what I think you were looking for.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rascalAuthor Commented:
Thanks julian!
0
Jason C. LevineNo oneCommented:
Nice answer, Julian...
0
Julian HansenCommented:
You are welcome - thanks for the points.
(nod to jason1178)
0
rascalAuthor Commented:
Hi Julian,

I tried your solution above but it doesn't appear to be working on my system.

After applying your code to the .htaccess file, I then went to my site and entered in the following address:

http://www.mydomain.com/nopage.html

I then added a line to the top of my index.php file:
echo "the page requested is " . trim($_REQUEST['page']);

But the result was that trim($_REQUEST['page']) was empty.

Am I missing something fundamental?

Thanks!
0
rascalAuthor Commented:
Update: The index.php page does display - but oddly the php handler does not get invoked - it gets displayed as if it had an extension of .html so all the php code in the page is just written out to the browser.
0
Julian HansenCommented:
Can you post your .htaccess here.
0
rascalAuthor Commented:
Hi Julian,
Below is the complete htaccess file used (it is just the htaccess file you first posted at the top of this thread:

AddType x-mapp-php5 .php
Options +FollowSymLinks

RewriteEngine on
RewriteBase /

# condition: file must not exist
RewriteCond %{REQUEST_FILENAME} !-f
# send it off to index.php
RewriteRule (.*).html$ index.php?page=$1 [L,NC,QSA]

Open in new window

0
Julian HansenCommented:
Ok will take a look.

Although it sounds like something on your server rather than in your script - that .htaccess works fine on all servers I use it.

You can also try this
.htaccess
# Make html files processed by default by php
AddType application/x-httpd-php .html .htm

Open in new window

You would then need to match the page name inside your index.php
0
rascalAuthor Commented:
Thanks Julian,
I'll also check with the web host to see if they have any overriding settings at the server level.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.