Page 1 of 1
URL re-writing without mod_rewrite
Posted: Fri May 30, 2008 5:24 pm
by joefazee
How can i rewrite a URL without apache mod_rewrite?
Sothing like
http://www.site.com/products?id=10&categoryid=20
To
http://www.site.com/products/10,20.html
If i use mod_rewrite, this is simple, i want to do without mod_rewrite, the new joomla 1.5 comes with something like that.
Re: URL re-writing without mod_rewrite
Posted: Fri May 30, 2008 5:33 pm
by Luke
Yea most php frameworks allow you to do this... if you want PURE php, the url will have to look like this though
http://www.example.com/index.php/products/10.20.html (not sure if comma is allowed or not and too lazy to check)
Otherwise you can point ALL requests to index.php with a mod_rewrite and they'll look like this:
http://www.example.com/products/10.20.html
Then you can parse the value of $_SERVER['path_info'] to grab the right php file and display it.
Re: URL re-writing without mod_rewrite
Posted: Fri May 30, 2008 5:39 pm
by joefazee
The Ninja Space Goat wrote:Yea most php frameworks allow you to do this... if you want PURE php, the url will have to look like this though
http://www.example.com/index.php/products/10.20.html (not sure if comma is allowed or not and too lazy to check)
Otherwise you can point ALL requests to index.php with a mod_rewrite and they'll look like this:
http://www.example.com/products/10.20.html
Then you can parse the value of $_SERVER['path_info'] to grab the right php file and display it.
I just need some explanation on it, I know about mod_rewrite, but i`m trying to use pure php.
Can use please show little example? Sorry for the time consumption
Re: URL re-writing without mod_rewrite
Posted: Fri May 30, 2008 5:55 pm
by Luke
Actually I'm sorry I dont think the .html part is possible in pure php w/out some special configuration (I didn't really think about it before) so your url would have to look more like:
http://www.example.com/index.php/products/10/20/
Let's assume your url looks like that... so save the following as index.php
Now when you go to the url mentioned above (remember to replace example.com with your domain name) the result should be "/products/10/20/" so use that to figure out what your script needs to do (this is generally called routing)
Code: Select all
$path = $_SERVER['PATH_INFO'];
$vars = explode("/", $path);
if (isset($vars[0]) && $vars[0] == 'products') {
include 'includes/products.php'; // then this file would handle the products page output
}
Re: URL re-writing without mod_rewrite
Posted: Fri May 30, 2008 7:10 pm
by joefazee
The Ninja Space Goat wrote:Actually I'm sorry I dont think the .html part is possible in pure php w/out some special configuration (I didn't really think about it before) so your url would have to look more like:
http://www.example.com/index.php/products/10/20/
Let's assume your url looks like that... so save the following as index.php
Now when you go to the url mentioned above (remember to replace example.com with your domain name) the result should be "/products/10/20/" so use that to figure out what your script needs to do (this is generally called routing)
Code: Select all
$path = $_SERVER['PATH_INFO'];
$vars = explode("/", $path);
if (isset($vars[0]) && $vars[0] == 'products') {
include 'includes/products.php'; // then this file would handle the products page output
}
I was afraid that because there is no directory called products/ the URL will return 404 error, but it went fine
This forum is the best..
It works like magic.
So what is the disadvantage? Why did people still struggle to use mod_rewrite?
I know about the advance pattern matching mod_rewrite can do, but this is simple.
Re: URL re-writing without mod_rewrite
Posted: Sat May 31, 2008 4:19 am
by Luke
Well actually most of the frameworks do use this method (or similar), but remove the name of the php file with mod_rewrite. With a rule like this, you could have the original scheme you requested (
http://www.example.com/products/10/20.html )
Code: Select all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
mod_rewrite is an awesome method if you don't have much control over the url structure like in miva merchant (a product I have the misfortune of being married to). It allows you to make up urls completely outside of the application and redirect them appropriately, but this way (php) is much better for portability and many other things.
Re: URL re-writing without mod_rewrite
Posted: Sat May 31, 2008 9:28 pm
by LSJason
The problem with using a non-mod_rewrite solution is the http://*/index.php/*/... URL structure. With mod_rewrite, one can send users to
http://www.mysite.com/profile.html instead of
http://www.mysite.com/index.php/profile. Therefore, mod_rewrite-based URLs are more memorable to the end user.