rascal
asked on
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!
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!
ASKER
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:
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]
ASKER
Here is what WordPress uses:
So I think if I just use that I should be good to go?
# 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
So I think if I just use that I should be good to go?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks julian!
Nice answer, Julian...
You are welcome - thanks for the points.
(nod to jason1178)
(nod to jason1178)
ASKER
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!
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!
ASKER
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.
Can you post your .htaccess here.
ASKER
Hi Julian,
Below is the complete htaccess file used (it is just the htaccess file you first posted at the top of this thread:
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]
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
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
You would then need to match the page name inside your index.php
ASKER
Thanks Julian,
I'll also check with the web host to see if they have any overriding settings at the server level.
I'll also check with the web host to see if they have any overriding settings at the server level.
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
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&
Becomes
index.php?page=contact&ful