woocha Posted May 11, 2008 Share Posted May 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/105151-replace-htaccess-file-with-php-function-please/ Share on other sites More sharing options...
wildteen88 Posted May 11, 2008 Share Posted May 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/105151-replace-htaccess-file-with-php-function-please/#findComment-538378 Share on other sites More sharing options...
woocha Posted May 11, 2008 Author Share Posted May 11, 2008 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 How would start setting that up? with a string replace or something? Quote Link to comment https://forums.phpfreaks.com/topic/105151-replace-htaccess-file-with-php-function-please/#findComment-538388 Share on other sites More sharing options...
shank888 Posted May 11, 2008 Share Posted May 11, 2008 I wouldnt mind learning more about mod_rewrite as well Quote Link to comment https://forums.phpfreaks.com/topic/105151-replace-htaccess-file-with-php-function-please/#findComment-538400 Share on other sites More sharing options...
wildteen88 Posted May 11, 2008 Share Posted May 11, 2008 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 /> Quote Link to comment https://forums.phpfreaks.com/topic/105151-replace-htaccess-file-with-php-function-please/#findComment-538407 Share on other sites More sharing options...
nloding Posted May 11, 2008 Share Posted May 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/105151-replace-htaccess-file-with-php-function-please/#findComment-538411 Share on other sites More sharing options...
rarebit Posted May 11, 2008 Share Posted May 11, 2008 A forum: http://forum.modrewrite.com/ A cheatsheet: http://www.ilovejackdaniels.com/cheat-sheets/mod_rewrite-cheat-sheet/ Quote Link to comment https://forums.phpfreaks.com/topic/105151-replace-htaccess-file-with-php-function-please/#findComment-538415 Share on other sites More sharing options...
wildteen88 Posted May 11, 2008 Share Posted May 11, 2008 Umm nloding and rarebit please read the OP question. We're not use mod_rewrite. The OP wanted to do this with just PHP. Quote Link to comment https://forums.phpfreaks.com/topic/105151-replace-htaccess-file-with-php-function-please/#findComment-538416 Share on other sites More sharing options...
nloding Posted May 11, 2008 Share Posted May 11, 2008 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 ... Quote Link to comment https://forums.phpfreaks.com/topic/105151-replace-htaccess-file-with-php-function-please/#findComment-538431 Share on other sites More sharing options...
owned Posted May 11, 2008 Share Posted May 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/105151-replace-htaccess-file-with-php-function-please/#findComment-538568 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.