0

I'm creating a simple php website with changeable themes. I have a directory called MyThemes with a a folder inside containing the following files header.php, sidebar.php, page.php, footer.php now the problem is that i want to display a page from the database using the page.php file of the selected theme, but the generated link will be something like

website/MyThemes/ThemeName/page.php?id=somePageID

I want to change that if possible to something like

website/pages/somePageID

I have a little experience with PHP, but apparently not enough to do this. So any help will be greatly appreciated.

2
  • use any framework like codeigniter,cakephp,etc Commented Nov 29, 2013 at 17:58
  • You can achieve this by modifying your .htaccess file. Twitter and Facebook do similar things to give each user a unique, readable URL. Commented Nov 29, 2013 at 18:00

2 Answers 2

2

This is typically done via URL rewriting by the webserver: the server makes sure /url/like/this is converted to (for instance) something.php?like=this. After that there is no difference to the application. Apache uses mod_rewrite to do this. If you were using Django, this would be configured in the urls.py files.

You could still simplify your URLs to website/pages.php?id=n by remembering the theme in a cookie or session variable though.

Also, your current file paths suggest that you duplicated the pages for the different themes though: this is never a good idea. Don't repeat yourself!

Sign up to request clarification or add additional context in comments.

8 Comments

what do you mean i duplicated the pages for the different themes?
your current naming scheme suggests that you have the exact same logic in ThemeA/page.php as in ThemeB/page.php.
So why is that a problem? i believe wordpress uses the same thing
note: only duping logic is bad. If your page.php only contains display code (like in an MVC View) it is fine.
the page.php includes the header.php the sidebar.php then the page content i get from the database an then the footer.php
|
1

You can use friendly urls and regular expressions:

See

http://www.phpriot.com/articles/search-engine-urls

In this article, you'll find instructions for using htaccess:

Example:

<IfModule mod_rewrite.c>
RewriteEngine on
#rule not apply directories
RewriteCond %{REQUEST_FILENAME} !-d
#rule not apply files
RewriteCond %{REQUEST_FILENAME} !-f

#Rule to page 
RewriteRule ^page/$ page.php
RewriteRule ^page$ page.php
RewriteRule ^page/(.*)/(.*)?$ page.php?id=$1&des=$2
RewriteRule ^page/(.*)?$ page.php?id=$1
</IfModule>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.