djr33
01-26-2010, 09:37 AM
Warning: This tutorial is a brief explanation of some very advanced concepts. It is not possible to do this without a solid understanding of general operations in PHP and some idea about how servers work in general. Use this at your own risk and always test first, hopefully on an extra server.
It is, however, designed to be easier than the (more) advanced methods typical of .htaccess, though it does not eliminate all complexity.
---
I have created a summary in this post:
http://www.dynamicdrive.com/forums/showpost.php?p=229295&postcount=5
Read the whole tutorial for a complete understanding, but all of the crucial information is there.
Also, it contains the finalized version, rather than the work-in-progress throughout some of the full tutorial.
---
I have been searching google for a simple answer to some of this for days and finally decided to go about this a simplified way that fits more into my area of knowledge: skip past the .htaccess/mod_rewrite and into php for the complex issues!
I thought it might help some others, or at least provide a brain teaser.
mod_rewrite is used to get rid of ugly links and make them pretty without compromising the structure of your website.
/index.php?var=value&var1=value1 is ugly.
/value/value1 is a lot nicer.
However, mod_rewrite is very complex. Obviously if you can figure out all of the details it is powerful and worth looking into-- but if you have trouble (like I have), then this is a very simple way to still get a lot of it's power.
First, you need to understand the very basics of mod_rewrite. This tutorial will not cover that. In short, google "mod rewrite" and find out how to turn it on. Once it is enabled on your server, you can control all of this complexity with .htaccess files.
A .htaccess file in the root (main) directory of your site will affect the entire site. If it is inside a subdirectory, it will only affect that and its internal subdirectories. You can decide what part(s) of your site you want to do.
Now, since we want to keep things simple, the idea is to redirect this to a php page that can handle everything there, rather than in the harder to work with htaccess.
For this, we will do a simple trick: send every single url to a single php page to decide what to do with it.
Here is an example .htaccess file inside the directory /test/:
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Now EVERY request within the folder "/test" will be "forwarded" to the index.php page.
This page can be very dynamic, so don't worry about your content being limited to a single page.
On /test/index.php, you can use this code:
<?php echo $_SERVER['QUERY_STRING']; ?>
The QUERY_STRING is the string that comes after the filename:
http://example.com/test/index.php?var=1 ---> "/?var=1"
http://example.com/test/index.php/var/1 ---> "/var/1"
Now, remember, this is going through a hidden redirect, so the browser will actually show:
http://example.com/test/?var=1
http://exmaple.com/test/var/1
This is much cleaner.
Everything is running through that single page, though, and what do you do now?
$_SERVER['QUERY_STRING'] will hold the information of the "file" they wanted to access after the /test/ directory.
You will need to use string functions, or regular expressions if you want to deal with that (it can be confusing if you don't know what you're doing).
You can do some very basic parsing of that string and then use PHP includes to reorganize your site.
In other words, now you have all of the information available in PHP through the Query String and you can use it to handle internal redirection instead of .htaccess. It is completely customizable to your needs.
---
This is meant just to get you started, but it's how I'm proceeding on a current project. mod_rewrite is messy and difficult-- something for the future-- and for now this will get the job done.
Notes:
1. If you DO NOT want a subdirectory, but instead your whole site, place that .htaccess file in the root (main) directory, then remove the line with "RewriteBase".
2. If you do make it site-wide (1), then you can actually create a php page-server for the entire site-- separate pages, separate directories and all served by include from a single hub page that decides what to do. Very powerful, though also complex.
3. Be Careful! Get variables, among other things, may be changed in unexpected ways. You can (though it is also messy and may get confusing) actually override the default values of $_GET, $_REQUEST, $_SERVER['QUERY_STRING'] (those globals that hold the value of variables from the URL). Again, Be Careful here!
4. This is in no way suggesting that it entirely replace mod_rewrite or a normal filesystem. However, it is a very simple way to approach the situation given that other options are difficult to implement without experience with some of the more advanced concepts behind them.
5. Important: to the end user, your site is actually being served from the URL that they use. Because of this, they will frequently be in another directory. Just like all options where the URL changes, you cannot use relative links: use absolute links, in the format "http://example.com/test.htm" or "/test.htm".
It is, however, designed to be easier than the (more) advanced methods typical of .htaccess, though it does not eliminate all complexity.
---
I have created a summary in this post:
http://www.dynamicdrive.com/forums/showpost.php?p=229295&postcount=5
Read the whole tutorial for a complete understanding, but all of the crucial information is there.
Also, it contains the finalized version, rather than the work-in-progress throughout some of the full tutorial.
---
I have been searching google for a simple answer to some of this for days and finally decided to go about this a simplified way that fits more into my area of knowledge: skip past the .htaccess/mod_rewrite and into php for the complex issues!
I thought it might help some others, or at least provide a brain teaser.
mod_rewrite is used to get rid of ugly links and make them pretty without compromising the structure of your website.
/index.php?var=value&var1=value1 is ugly.
/value/value1 is a lot nicer.
However, mod_rewrite is very complex. Obviously if you can figure out all of the details it is powerful and worth looking into-- but if you have trouble (like I have), then this is a very simple way to still get a lot of it's power.
First, you need to understand the very basics of mod_rewrite. This tutorial will not cover that. In short, google "mod rewrite" and find out how to turn it on. Once it is enabled on your server, you can control all of this complexity with .htaccess files.
A .htaccess file in the root (main) directory of your site will affect the entire site. If it is inside a subdirectory, it will only affect that and its internal subdirectories. You can decide what part(s) of your site you want to do.
Now, since we want to keep things simple, the idea is to redirect this to a php page that can handle everything there, rather than in the harder to work with htaccess.
For this, we will do a simple trick: send every single url to a single php page to decide what to do with it.
Here is an example .htaccess file inside the directory /test/:
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Now EVERY request within the folder "/test" will be "forwarded" to the index.php page.
This page can be very dynamic, so don't worry about your content being limited to a single page.
On /test/index.php, you can use this code:
<?php echo $_SERVER['QUERY_STRING']; ?>
The QUERY_STRING is the string that comes after the filename:
http://example.com/test/index.php?var=1 ---> "/?var=1"
http://example.com/test/index.php/var/1 ---> "/var/1"
Now, remember, this is going through a hidden redirect, so the browser will actually show:
http://example.com/test/?var=1
http://exmaple.com/test/var/1
This is much cleaner.
Everything is running through that single page, though, and what do you do now?
$_SERVER['QUERY_STRING'] will hold the information of the "file" they wanted to access after the /test/ directory.
You will need to use string functions, or regular expressions if you want to deal with that (it can be confusing if you don't know what you're doing).
You can do some very basic parsing of that string and then use PHP includes to reorganize your site.
In other words, now you have all of the information available in PHP through the Query String and you can use it to handle internal redirection instead of .htaccess. It is completely customizable to your needs.
---
This is meant just to get you started, but it's how I'm proceeding on a current project. mod_rewrite is messy and difficult-- something for the future-- and for now this will get the job done.
Notes:
1. If you DO NOT want a subdirectory, but instead your whole site, place that .htaccess file in the root (main) directory, then remove the line with "RewriteBase".
2. If you do make it site-wide (1), then you can actually create a php page-server for the entire site-- separate pages, separate directories and all served by include from a single hub page that decides what to do. Very powerful, though also complex.
3. Be Careful! Get variables, among other things, may be changed in unexpected ways. You can (though it is also messy and may get confusing) actually override the default values of $_GET, $_REQUEST, $_SERVER['QUERY_STRING'] (those globals that hold the value of variables from the URL). Again, Be Careful here!
4. This is in no way suggesting that it entirely replace mod_rewrite or a normal filesystem. However, it is a very simple way to approach the situation given that other options are difficult to implement without experience with some of the more advanced concepts behind them.
5. Important: to the end user, your site is actually being served from the URL that they use. Because of this, they will frequently be in another directory. Just like all options where the URL changes, you cannot use relative links: use absolute links, in the format "http://example.com/test.htm" or "/test.htm".