I was not sure how to word this, so I am going to try to explain as well as I can. I have written a plugin system in PHP for a CMS. However, the plugins are contained within their own folder, a page that came with a plugin might look like this:
http://www.mysite.com/plugins/forum/index.php
However, this looks ugly and obviously exposes the site structure of the plugin system. So far I have been getting around this problem like so:
http://www.mysite.com/page.php?id=forum
but that is still kind of ugly even though its a little cleaner than the latter. However, I was wondering if it is possible to create a "dummy" page which is actually just an alias for the page I want. So
http://www.mysite.com/forum.php points to http://www.mysite.com/plugins/forum/index.php
While I can create a page in the root forum.php manually, I was wondering if there was some way to automate it. In other words, when the user calls forum.php which doesn't exist, I have a 404 page hold a piece of PHP code which figures out what is being called (throwing a 404 if its an unknown page call). In other words, if I request forum.php, a piece of code consults the database to find the linker and if it does, spits back the contents of the page under that specific URL.
I hope this is not too confusing. Let me know if its a bit hard to understand the idea... I would greatly appreciate any help!
If you are using apache, the solution is to use mod_rewrite (.htacces)
.htaccess tricks and tips
The idea is to redirect internally to your plugin when the user accesses
http://www.mysite.com/forum.php
Ex: you place this in your .htaccess file in your webroot
RewriteEngine on
RewriteRule ^forum\.php$ /plugins/forum/index.php.php [QSA]
Related
I need to put together a simple/small website fairly quickly for internal use (no external access), which will contain a few forms and simple interactive pages.
I don't really want the maintenance overhead of a CMS, but so that each page has a consistent look and feel, etc, it would be useful to be able to have each page be based on a common template to wrap around the unique page content, to include the HTML head, title, site navigation, footer, etc.
One approach would be to include various snippets via PHP within each individual page, but that involves repetition in each page, and doesn't scale well if I decide I might need to substantially change things later.
The alternative approach would be to use the main DocumentRoot index.php file as the template, and instead have it include the requested page 'within' itself (so that each of the other pages is actually really only a partial file defining variables for the 'title' and 'body' (for the main page body content)).
I see that I can use $_SERVER['PATH_INFO'] to extract the desired file path (relative to the DocumentRoot) and $_SERVER['REQUEST_URI'] to get the whole request string (in case there might be any GET parameters); for the actual content of the index page itself I could have it include an alternatively-named file instead; and there must be some way in Apache rewrite rules that it would be possible to elide out the index.php from the eventual URIs, ..but I haven't yet thought through very much further than this.
I am sure that this must be a scenario encountered many many times before. I could well spend a couple of days trying to think this through and re-invent my own wheel, but I don't really have the time to do this, and it probably wouldn't be a very good use of it in any case.
Does anybody have some existing "quickie" code that would be "good enough" for this, or know of something "formally" published (at which point does a working quick hack become an actual software package?!)?
Thanks for any advice.
You could send your requested page as url parameter to your index.php, like 'index.php?page=requested_page' and include the following rule in your index.php at the position where you like to get your content
<?php
include("./pages/".$_GET['page'].".php");
?>
I'm not sure if any effort you will put into this will be less than the effort of implementing a simple CMS. Most systems have simple setups and do most of the work for you. Any time you save on not implementing a CMS will cost you later on maintenance. Don't use 'internal use' as an excuse to make bad software. Unless it's a one-time solution that will be disregarded in a few weeks, you (or another developer) will have to extend and maintain the software.
Redirect all requests to index:
RewriteEngine on
RewriteBase /
RewriteRule ^(?!index\.php$|(?:css|js|media)(?:/|$)). index.php [L]
Something like that will stop redirect loops on the index and send everything except requests to static content folders (change to suit) to your handler in the index. Access the URL via $_SERVER['REQUEST_URI'].
I am a beginner PHP programmer. I searched google for a "Dynamic PHP website tutorials". I found some stuff. They use $_GET variable to make the website dynamic, so the URL's appear like this:
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
and so on...
But most of the dynamic websites that I found on the internet has links like this:
example.com
example.com/about
example.com/download and so on....
So how do they do so ?? Have they got folders for all the catogories ?? And Also some websites have article URLs (eg : example.com/articles/posts/2010/article1.php). It would be a reall mess if they've got folders for all items. If not then How ?? Can someone give an example please ?
If you're using apache then read: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you're using IIS then read: http://www.iis.net/downloads/microsoft/url-rewrite
In order to use the $_GET variable, it must be in the query string (or being routed through some other means that isn't 'default').
For example, the URLs you're using would become.
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
Additionally, you can rewrite URLs using the .htaccess file (http://httpd.apache.org/docs/2.0/misc/rewriteguide.html)
You are interested in page routing.
htaccess and MVC routing may start you down the correct path :)
To echo everyone else, it's called a url rewite.
For example, the url
http://example.com/index.php?ext=blog&cat=news&date=12122012
can be rewritten as
http://example.com/blog/news/12-12-2012
This isn't automatic, it requires defining the patterns used for understanding the new URL in a file called .htaccess which usually resides in the servers root directory. Note that the preceding '.' in the filename makes it a hidden file.
When I was first getting used to PHP i found the site http://phpbuilder.com a great help. They have a lot of articles, and a forum that is fairly nice to noobies. http://devshed.com is a good site too, and has a large amount of information on subjects outside of PHP.
You can achieve that affect with folders, but most use rewrites (Apache). It's a bit too broad of a subject to go in to here, but if you just search for rewrite tutorials you'll find some pretty quickly.
The $_GET is only to get variables from the URL. While this can be used to make sites dynamic, this is a technique which is usually frowned upon.
With rewrites, you basically have a URL like /about, but the rewrite tells your server something like "act like this is actually ?page="about"), which you then use the $_GET to process.
Being PHP beginner I will not urge you to use .htaccess, As you will need to learn lot many things before you proceed further. You have 2 option to send a request one is GET and POST. You can get more information about same on internet.
Also you have an option to start your dynamic website using CMS and I will recommend you to use wordpress. CMS will have some in-built function which will help you to do your work faster. Also using their control panel you can update the URL format.
I will also urge you to go step by step and follow every tutorial that you will find on internet.
All the best
If you want to do this you have to use .htaccess file and have to load mod_rewrite in your apache server.
In you root directory create file named .htaccess
Then Write:
RewriteEngine On
RewriteRule ^(.*)\.php$ index.php?page=$1 [L,QSA]
And After that call a page
my-page.php
It will redirected as a index.php?page=my-page internally but in browser it will show as my-page.php
I am buildiig a simple CMS and would like to know how to create short URLs (not the APACHE bit but the PHP bit).
example.com/?page=100
example.com/home/test
How would I interpret the ?page=100 into /home/test (Through select the database, but i couldn't figure out how) I can see if just one level /home/test because you probably can have a zoneID, but when it comes to /home/test/test. I become lost
And how do I parse back the /home/test to the page id.
Plus is there anyone can show a bit idea for the database design as well?
These resources can be useful to you:
https://stackoverflow.com/a/120411/370290
http://www.symfony-project.org/book/1_0/09-Links-and-the-Routing-System
http://codeigniter.com/user_guide/general/urls.html
http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/
You need some kind of mod_rewrite for your server side.
That will help you to send route data to index.php (or somewhere else) file without filename in adress string. Than some php file will analyze the route and give correct html.
ok i think you need to definitely need to look at the way you are going to do your routing (through mod_rewrite)..for example
1.you can rewrite the page www.example.com/test to ..www.example.com/index.php?page=test and implement a way of getting page by the page name..and returning an id if a page name exists ..if multiple entries exist then maybe the last modified will be given precedence over the otheers ..you can get the following book CMS Design Using PHP and jQuery helped me alot
Wordpress uses friendly seo url without using htaccess.
Can any explain this to me please how they do it.
The only way i can think of is to do something like this.
domain.com/index.php/nnn/mmmm/
But wordpress does not use index.php
I know they are not using htaccess.
Please let me know.
Thank you.
WordPress actually has a single .htaccess file that they don't need to change which redirects all requests to index.php
index.php then looks at the permalinks rules and runs a few database queries to determine which page to send you.
So for instance if the permalink rule is %postdate%/%postname% (may not be the actual WordPress permalink variables. I haven't been using WordPress for too long) then it would just use regular expressions (or combinations of substr() and strpos()) to put %postdate% and %postname% into variables. Next it runs a simple database query for any item matching that date and that name. If nothing is found, redirect to search. If you find more than one, list them all (like a category page). If you find one and only one, send that page.
As far as actually "sending" the page, that's just a matter of settings certain variables (such as $the_post['content']) and then include()'ing the proper theme file.
include()'ing the theme file is a simple if() statement.
if(file_exists("wp-content/themes/<your_theme>/$the_post['type'].php")){
include("wp-content/themes/<your_theme>/$the_post['type'].php");
} else {
include("wp-content/themes/<your_theme>/index.php");
}
Mind you these aren't the exact variable names or the exact functions as they occur. This is just a very simplified version to give the general idea of how these systems work.
Wordpress does use .htaccess files. You can modify the permalinks in the permalinks section your wordpress site. You can see information on it here.
If you are using IIS you can use this method
Wordpress does actually use .htaccess. Make sure you have hidden files displayed! If you're FTP-ing, there'll be a setting in your client.
To answer the main question:
No.
As for the other points of yours:
Wordpress uses friendly seo url without using htaccess.
USUALLY it does not.
USUALLY it requires the .htaccess to make nice urls work.
However, as Wordpress' Codex says:
If you are using IIS 7 and have admin rights on your server, you can
use Microsoft's URL Rewrite Module instead. Though not
completely compatible with mod_rewrite, it does support WordPress's
pretty permalinks.
So they are always using some kind of rewrite module anyway.
Can any explain this to me please how they do it.
There is one interesting thing to explain:
There is only one .htaccess file.
Which would not be so interesting if it would contain some kind of general rule rewriting urls to some.php's GET parameters (domain.com/verynice => domain.com/index.php?page=verynice).
But that is not happening.
What is happening is that
It rewrites ANY url that is NOT a specific FILE or DIRECTORY to index.php EXCEPT the index.php (to prevent infinite loop).
So urls like domain.com/blah/blah and domain.com/lol/trololo are both actually requesting the index.php - the only difference is in the requested URI.
Using superglobal variables like $_SERVER['REQUEST_URI'] they get the requested URI and after that they parse the requested URI and store the results in varibles accordingly.
The only way i can think of is to do something like this.
domain.com/index.php/nnn/mmmm/
In common explanation of the URL this is NOT request to the index.php file itself.
It is a request for the "mmmm" folder in folder "nnn" which is in folder "index.php".
However apache usually understand this as a request for the index.php file .
For other files e.g. my.file domain.com/my.file is a file request, domain.com/my.file/ is a folder request (notice the slash / at the end)
But wordpress does not use index.php
That is NOT TRUE.
Especially if you are using nice-urls the index.php is practicaly fundamental for Wordpress.
I know they are not using htaccess.
This point of yours could meant 3 things:
They are using the htaccess but not for processing the URL (which I've explained above)
They are not using it at all - not true, unless they would be using the MS' URL Rewrite Module
You don't know. You did not provide any information about how did you find out they are not using it, so we can't know if you really know or you just think that :)
Please let me know.
I let you know. And I hope I've answered all your questions :), if not - use comments :)
Thank you.
You are welcome.
I was happy to help :) ☺ ☺ ☺ ☺ ☺
My site currently handles URL's like this...
/?p=home
/?p=profile&userid=1
/?p=users.online
/?p=users.online&page=23
/?p=mail.inbox
/?p=mail.inbox&page=12
... and so on, there is probably at least 120-150 different pages, on my site a page is built like this,
index.php includes a main config file which then includes function/class files into it
index.php then includes a header file
index.php then includes the page file which is from the url ?p=pagename
index.php then includes a footer file
That is how every page on my site is compiled, through the index page like that and I have been considering/thinking about cleanning up the URL's as I am re-writing/restructuring most of my sites code right now, it's the perfect time to do it if I am going to do it. What I mean by cleanning up the URL's is using mod-rewrite so the URL structure above would look like this instead...
/home
/users/1 //user ID might switch to use username as well
/users/online
/users/online/23 or /users/online/page/23
/mail/inbox
/mail/inbox/12
So first of all is there any downfalls to doing this, does it create a lot more processing work since it is using mod_rewrite?
Also would it be hard to write the regex or whatever is needed to match the file names in the format that I show above, I listed only a few of the pages but there would be at least 100 different URL pages I have blogs, bulletins, forums, all kinds of stuff
Absolutely. That's one of Apache's main purposes, so it's been designed to do it very efficiently. In fact, I'd emplore you to use that method.
It makes cleaner URLs for visitors
It's fantastic for SEO
It makes the website more professional
It makes it easier for users to guess URLs if they are trying to find a certain page without actually traversing through your site ti find it.
There are no downfalls, and a ton of advantages to using that URL structure. Go for it!