Jump to content

replace .htaccess file with php function please


woocha

Recommended Posts

Hey guys, 

 

    I have an .htaccess file that I use for pretty URL rewrites.  I would like to accomplish the same thing, but with a php function.  My purpose for thie is to be able to give my clients the ability to turn it on and off.  I can do that with a function but not with an htaccess file.  Below is a copy of my htaccess file.  Does anyone have any suggestions?

 

Options +FollowSymLinks
RewriteEngine on
RewriteRule details-merch-(.*)-itm_num-(.*)\.htm$ details.php?merch=$1&itm_num=$2

Why don't you create a core function in your app which will generate either clean urls or default messy urls if users want to use mod rewrite. This is how most PHP apps are set up.

 

However you can do friendly clean urls with just PHP but the file name must be present. The cleanest url you'll be able to get is:

filename.php/clean/url/here

I'd do it like this

<?php

if(isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != '/' && count($_GET) == 0)
{
    // get the full url.
    $path = $_SERVER['PATH_INFO'];

    // explode the url into pieces.
    // eg filename.php/piece1/piece2/etc
    $path_bits = explode('/', $path);

    // remove the file name from $path_bits array
    array_shift($path_bits);

    // loop throught the remaining values in the $path_bits array
    // we'll automaticlly populatue the $_GET superglobal array
    foreach($path_bits as $bit)
    {
        // each piece contains both the key and the value eg:
        // filename.php/key1-value1/key2-value2/etc
        list($key, $value) = explode('-', $bit);

        // populate the $_GET superglobal array
        $_GET[$key] = $value;
    }
}

echo '<pre>' . print_r($_GET, true) . '</pre>';

?>
<hr />
TESTS

<a href="<?php echo $_SERVER['SCRIPT_NAME']; ?>/id-1">TEST1 Clean</a> | <a href="<?php echo $_SERVER['SCRIPT_NAME']; ?>?id=1">TEST1 Default</a><br />
<a href="<?php echo $_SERVER['SCRIPT_NAME']; ?>/somevar-hello/anotherver-abc123">TEST2 Clean</a> | <a href="<?php echo $_SERVER['SCRIPT_NAME']; ?>?somevar=hello&anotherver=abc123">TEST2 Default</a><br />

For help with mod_rewrite, I found this site extremely helpful recently:

 

http://www.workingwith.me.uk/blog/software/open_source/apache/mod_rewriting_an_entire_site

 

There is also a link in the 3rd or 4th paragraph to his "beginner's guide" to mod_rewrite.

 

As to replacing the functionality of the .htaccess file with a PHP function.  Just make an option in the Options page to prettify the URLs or not.  Have that set a constant, and when creating a link, just do a quick CONSTANT ? pretty link : non-pretty link.

 

See Wordpress.  Best example I can think of right now.

wildteen88 -- read my whole post, I gave a suggestion :)

 

My understanding is was that he was looking for a way to do something similar with out .htaccess to give users the option to install the mod_rewrite module or not, but still leave the option of using mod_rewrite.  I might've read it wrong though ...

I would recommend to make every request via index.php and set up pretty urls there. Inside index.php include() can be used when having multiple files.

 

 

 

 

Simple example with mod-rewrite:

 

#.htaccess
RewriteEngine On 
RewriteRule ^([a-zA-Z0-9\-_'!;\[\]()=]+)(\.html)?$ ?short=$1 [QSA,L]

#also this should be used if doing things below
<Files ~ "\.inc$">
Order allow,deny
Deny from all
</Files>

 

<?php
$short=$_GET['short'];
$short_exp=explode('-',$short);
$page=$short_exp[0];
$sub=$short_exp[1];

//here include  can be used and put codes below into separate files e.g. include($page.".inc");
if($page=='homepage'){
echo "homepage";
if($sub=='1'){
	echo "subthings";
}
if($sub=='2'){
	echo "subthings2";
}

}
if($page=='info'){
echo "info";
}
?>

 

Usage :

http://domain.com/homepage.html

http://domain.com/homepage-1.html

http://domain.com/homepage-2.html

http://domain.com/info.html

-or-

http://domain.com/homepage

http://domain.com/homepage-1

 

 

 

 

 

 

Without mod-rewrite above can be achieved like this:

 

<?php
$qstring=$_SERVER['QUERY_STRING'];
$qstring_exp=explode('-',$qstring);
$page=$qstring_exp[0];
$sub=$qstring_exp[1];
include($page.".inc");
?>

 

Usage:

http://domain.com/?homepage

http://domain.com/?homepage-1

 

*note the ?-mark

 

 

 

 

 

Switching between "mod-rewrite and php" I think every link must be printed either way. E.g.:

<?php
$linkstyle=$_COOKIE['linkstyle'];
if($linkstyle=='modrewrite'){
$link="/homepage-1.html";
}elseif($linkstyle=='querystring'){
$link="/?homepage-1";
}else{
$link="/?page=homepage&subpage=1";
}
echo "<a href='{$link}'>aaa</a>";
?>

 

 

 

 

 

 

To detect which url style is used, an example:

<?php
$getpage=$_GET['page'];
if($getpage==''){
$qstring=$_SERVER['QUERY_STRING'];
$qstring_exp=explode('-',$qstring);
if($qstring==''){
	$short=$_GET['short'];
	$short_exp=explode('-',$short);
	if($short!=''){
		$page=$short_exp[0];
		$sub=$short_exp[1];
	}else{
		echo "homepage";
	}
}else{
	$page=$qstring_exp[0];
	$sub=$qstring_exp[1];
}
}else{
$page=$getpage;
$sub=$_GET['sub'];
}

//etc.

?>

 

 

 

 

 

 

Hope this helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.