Forum Moderators: phranque
I've searched the forums and found a lot of mod-rewrite examples but I can't seem to find the one that I need. Can someone please show me how to write this.
I need a mod_rewrite that will make dynamic urls look static.
EXAMPLE:
[mydomain.com...] (p.s. have 100's of CatID)
from requests for
[mydomain.com...]
Thanks a lot
There are a number of threads discussing this already and we're not the Apache help desk! ;) We avoid redundent posts whenever possible and prefer to help you debug your code, not write it for you.
These pages should provide some good basics about your question to get you started:
[sitepoint.com...]
[evolt.org...]
I need a mod_rewrite that will make dynamic urls look static.
I think you are confuse about mod_rewrite...it doesn't make your dynamic URL look static but rather if somebody access your site say from [mydomain.com...] then mod_rewrite will convert this request into your acceptable dynamic URL say [mydomain.com...] otherwise the request would trigger a 404 error.
To make your dynamic URL look static, you have to hack the application/software that produces these dynamic URLs into seemingly static URL. This convertion has nothing to do with mod_rewrite.
The exact process on how to change for example [mydomain.com...] into [mydomain.com...] is dependent on the software you are using.
If there's a support board for your software, that's the best place to get an idea how to convert the URL, it's possible that somebody have develop the hack or module for that process.
Hope this help.
Cheers
Thank you for the urls.
i have checked [evolt.org...]
and flowed the authors insrructions.
firstly modifing the .htaccess to process a file as a file.php
<Files name-of-file>
ForceType application/x-httpd-php
</Files>
secound step; i used his function to expolde the url:
function processURI() {
global $REQUEST_URI;// Define our global variables
// $array = explode("/",$REQUEST_URI);// Explode the URI using '/'.
$num = count($array);// How many items in the array?
$url_array = array();// Init our new array
for ($i = 1 ; $i < $num ; $i++) {// Insert each element from the
$url_array["arg".$i] = $array[$i];// request URI into $url_array
}// with a key of argN. We start $i
// at 1 because exploding the URI
// gives us an empty ref in $array[0]
// It's a hacky way of getting round it
// *:)
return $url_array;// return our new shiny array
}
// displayContent():
function displayContent($array) {
$section = $array["arg3"];// get the values out of the array and
$cat = $array["arg4"];// assign them.
$content = "content/" // cat together all our elements to
. $section // get a file name.
. "_"
. $cat
. ".php";
if (!file_exists($content)) {// Does the file exist?
$content = "error.php";// Nope, someone is playing around
include($content);
} else {
include($content);// Yes, include the file
}
}
///////// END OF FUNCTIONS /////////////
?>
<?
// Init the page, run processURI and assign it.
$myarray = processURI();
?>
First: Why /directory/CatID=3 when you can have just /directory/3/?
Of course, /directory/name_or_keyword would make even more sense.
Anyway -- these two lines in /directory/.htaccess
RewriteEngine on
RewriteRule ^([0-9]+)/?$ ViewCat.php?CatID=$1
turn
[mydomain.com...] or
[mydomain.com...]
invisibly into
[mydomain.com...]
and work like that with every other number too.
If you have mod_rewrite on your server (you should), there's no need for complicated PHP.
Of course as Net_Wizard said you have to change all the links to point to the new alias address.
If you have access to the server error logs, see whether it logs something like this:
[Tue Jul 29 16:18:46 2003] [alert] [client 127.0.0.1] /.htaccess: Invalid command 'RewriteEngine', perhaps mis-spelled or defined by a module not included in the server configuration
Or make a PHP page with phpinfo(); and ctrl+f that for "mod_rewrite". If it's not there, you don't have it unfortunately.
Options +FollowSymLinks Some hosted sites require it in .htaccess before using mod_rewrite and some don't, depends on the server configuration.
Also, if you're new to mod_rewrite, you may find this intro [webmasterworld.com] useful.
[webmasterworld.com...]