Love2c0de Posted July 27, 2013 Share Posted July 27, 2013 (edited) Good evening,I have a website using php to set the pages up. My URL is showing as: http://localhost/Gardenable/?page=home for example.How can I change it so that it displays as: http://localhost/Gardenable/HomeKind regards,L2c Edited July 27, 2013 by Love2c0de Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/ Share on other sites More sharing options...
jazzman1 Posted July 27, 2013 Share Posted July 27, 2013 (edited) Good morning The word should start with a capital letter or it doesn't matter? If you want to start with the same word, you would use that one: RewriteEngine On RewriteCond %{REQUEST_URI} ^/Gardenable/$ RewriteCond %{QUERY_STRING} ^page=([a-z]+)$ RewriteRule ^(.*)$ http://localhost/Gardenable/%1 [R,L] Edited July 27, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442448 Share on other sites More sharing options...
Love2c0de Posted July 27, 2013 Author Share Posted July 27, 2013 Good evening, Â Thanks for your reply. Â I would prefer it to start with a capital letter yes. Â Do I need to put that into a file and upload it to my webhost? If so, where abouts would I upload it to? Â Kind regards, Â L2c Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442452 Share on other sites More sharing options...
jazzman1 Posted July 28, 2013 Share Posted July 28, 2013 Hey @Love2c0de, take a look at this good article, especially the part contains "mod-rewrite with Regular Expressions". http://www.sitepoint.com/apache-mod_rewrite-examples/ jazz. Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442467 Share on other sites More sharing options...
Love2c0de Posted July 28, 2013 Author Share Posted July 28, 2013 I'll give it a read now.  Thanks for the information.  Kind regards,  L2c. Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442478 Share on other sites More sharing options...
Love2c0de Posted July 28, 2013 Author Share Posted July 28, 2013 (edited) Just to let you know that I put your above code into the .htaccess file on my host and it was saying there was a 'crossnetwork' error?  I'll give that a read though as it might explain in there.  Edit: Damn host doesn't allow mod_rewrite   Regards,  L2c. Edited July 28, 2013 by Love2c0de Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442479 Share on other sites More sharing options...
Love2c0de Posted July 28, 2013 Author Share Posted July 28, 2013 When moving my database and tables to another host, do I just export it and then import to the new host? Â Kind regards, Â L2c. Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442481 Share on other sites More sharing options...
jazzman1 Posted July 28, 2013 Share Posted July 28, 2013 When moving my database and tables to another host, do I just export it and then import to the new host? Â Kind regards, Â L2c. Â Absolutely. Â Are you sure that your hosting provider doesn't allow to use mod_rewrite? Â jazz. Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442493 Share on other sites More sharing options...
jazzman1 Posted July 28, 2013 Share Posted July 28, 2013 Hey L2c,take a look for a minute at next example, which I created for you.The web structure is:   / - web root directory /cgi-bin/ - the folder contains display.php and .htaccess files /cgi-bin/display.php /cgi-bin/.htaccess  So, all URLs provided for users look like: /cgi-bin/USA/California/San_Diego /cgi-bin/USA/Massachusetts/Boston /cgi-bin/CANADA/Ontario/Mississauga There is no an URL, like - /cgi-bin/display.php?country=USA&state=California&city=San_Diego  If some user clicks on the link, for instance, http://domainName.ca/cgi-bin/USA/California/San_Diego, he will reach the file - /cgi-bin/display.php?country=USA&state=California&city=San_Diego without seeing this ugly URL.  You can make your own test to analyse, what's going on behind the hood.Test - http://canada.lesno.net/cgi-bin/display.php 1. display.php <?php if (!isset($_GET['country'])) { echo '<a href="./USA/California/San_Diego">Link 1</a><br />'; echo '<a href="./USA/Massachusetts/Boston">Link 2</a><br />'; echo '<a href="./CANADA/Ontario/Mississauga">Link 3</a>'; } else { echo '<pre>' . print_r($_GET, true) . '</pre>'; } 2. .htaccess RewriteEngine on RewriteBase /cgi-bin/ RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)$ display.php?country=$1&state=$2&city=$3 [L] Hope that wil help someone! PS: You would create your own RegEX to satisfy your personal whishes.  Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442516 Share on other sites More sharing options...
Love2c0de Posted July 29, 2013 Author Share Posted July 29, 2013 Thank you very much. Â The website link that you gave me though, there was a bit about checking whether your host allows mod_rewrite by doing php_info(); in a file and checking the apache modules, but nothing came up about mod_rewrite. Â I am on a free host, I think that's probably why? Â Regards, Â L2c. Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442579 Share on other sites More sharing options...
jazzman1 Posted July 29, 2013 Share Posted July 29, 2013 Ask them! Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442587 Share on other sites More sharing options...
Love2c0de Posted July 29, 2013 Author Share Posted July 29, 2013 ^^ That could be a good idea... Â I will, I just thought it was disabled or not allowed as when I put your code in my .htaccess file, it gave an error. Â Will report back! Â Regards, Â L2c. Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442612 Share on other sites More sharing options...
Love2c0de Posted July 29, 2013 Author Share Posted July 29, 2013 (edited) Awesome, it is enabled.  When I add your code to the .htaccess file in htdocs, the string in the URL is: http://labtec.0fees.net/Gardenable/home?page=home  Any thoughts?  Kind regards,  L2c Edited July 29, 2013 by Love2c0de Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442617 Share on other sites More sharing options...
jazzman1 Posted July 30, 2013 Share Posted July 30, 2013 (edited) The link of your home page 'http://www.labtec.0fees.net/Gardenable/?page=home' is equivalent "http://www.labtec.0fees.net/Gardenable/index.php?page=home"So if you want this link to be much more pretty something like - 'http://www.labtec.0fees.net/Gardenable/Home/' go to .htaccess and rewrite a rule.Assuming, this file is located inside Gardenable directory. RewriteEngine on RewriteBase /Gardenable/ RewriteRule ^/?([a-zA-Z_]+)$ index.php?page=$1 [L PS: That link contains an invalid URL - 'http://localhost/Gardenable/home?page=home' Edited July 30, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442657 Share on other sites More sharing options...
Love2c0de Posted July 30, 2013 Author Share Posted July 30, 2013 Good evening, Â Thank you for your reply. Â I put that in my .htaccess but it keeps taking me to a securesignup page @ http://securesignupoffers.org/g/ Â One of the template files are called contact_us, do I have to compensate for that within the reg exp? Â Regards, Â L2c. Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442726 Share on other sites More sharing options...
jazzman1 Posted July 31, 2013 Share Posted July 31, 2013 You are doing something wrong. Â The regular expression is just a pattern that describes a set of strings, so you can compensate that Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1442768 Share on other sites More sharing options...
Love2c0de Posted August 2, 2013 Author Share Posted August 2, 2013 What I mean is that I will have to change the RegExp to accommodate the underscore in contact_us? Â I'm not quite sure where I'm going wrong and still haven't got to grips with it. Â Can i activate this on localhost so I can test it locally? It's a pain navigating through my host. Â Kind regards, Â L2c. Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1443098 Share on other sites More sharing options...
jazzman1 Posted August 2, 2013 Share Posted August 2, 2013 Of course, you can made the tests on the local machine. If the version of your apache server is not the same like remote hosting maybe some part of the script and you need to check the manual of apache web site. Â So, to be more spesific why your own test doesn't work, you should provide us all scripts and the structure of the directories on apache server as well. Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1443186 Share on other sites More sharing options...
Love2c0de Posted August 3, 2013 Author Share Posted August 3, 2013 (edited) Good evening,  I have a main index.php script which uses php at the very top to determine which template file to load into my #container div, depending on the $_GET value passed.  Here it is: <?php include("core/init.inc.php"); //array containing the possible values of the form select lists, also used to select a specific product to display only information relating to //that product (on the products page). $get_values = array("bird_housing","planters","default_product","default_service","decking",          "fencing","exterior_buildings","furniture_repairs","jet_washing"); if(isset($_POST['name'])){            require("core/process_form.php");    } if (isset($_GET['page']) && $_GET['page'] == "products") {     if (isset($_GET['order'])){         if(in_array($_GET['order'],$get_values)){        require("core/get_products.php");     }     else{        header("Location: index.php?page=products");       die();     }   }   else{      require("core/get_products.php");   } } if(isset($_GET['page']) && $_GET['page'] == "display"){    if(!empty($_GET['id'])){       require("core/get_specific_product.php");    }    else{       header("Location: index.php?page=home");       die();    } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>    <title><?php echo "Gardenable - ".$title; ?></title>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />    <link rel="stylesheet" type="text/css" href="css/style.css" />    <link rel="stylesheet" type="text/css" href="css/font_style.css" />    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> </head> <body> <div id="container">   <!--HEADER CONTENT-->   <div id="header">      <img src="images/gardenable1.fw.png" alt="Gardenable Logo" title="Gardenable" id="logo" name="top" border="0" />         <div id="navigation_div">        <img src="images/flowerbed.fw.png" alt="Navigation Image" id="flowerbed_img" border="0" />       <ul>           <li><a href="?page=home">Home</a></li>           <li><a href="?page=about">About</a></li>           <li><a href="?page=products">Products</a></li>           <li><a href="?page=services">Services</a></li>           <li><a href="?page=contact_us">Contact Us</a></li>       </ul>     </div>   </div>   <!--END HEADER CONTENT-->     <!--BREADCRUMB DIV-->   <div id="breadcrumb_div">   <?php print("<p id='crumb'>".$crumb."</p>"); ?>   </div>   <!--END BREADCRUMB-->     <!--MAIN CONTENT-->    <?php include($include_page); ?>   <!--END MAIN CONTENT-->      <!--FOOTER CONTENT-->   <div id="footer">      <p id="copyright">               <span class="yellow">Tel:</span> 01255 722 960<br />               <span class="yellow">Email:</span><a href="mailto:gardenable@hotmail.com"> gardenable@hotmail.com</a><br />               <span class="yellow">Gardenable.com</span> <span style="font-size: 18px;">©</span> Copyright 2013</p>     <p id="footer_content">       <img src="images/css_validated.jpg" alt="CSS Validated" title="This site is CSS Validated." id="css_icon" />        <img src="images/html_validated.jpg" alt="HTML Validated" title="This site is HTML Validated." id="html_icon" />      </p>     <hr class="form_hr" style="margin: 0%;" />   </div>   <!--END FOOTER CONTENT-->   </div> <p id="pageviews"><?php echo "Page Hits: ".$page_views; ?></p> <div id="sidebar">    <a href="#top">Back to top</a> </div> </body> </html> The very first line is an include() statement. That init.inc.php file is what sets up the page to display, the breadcrumb effect, the page views and the title. Here is the code: <?php if(empty($_GET['page'])){   header("Location: ?page=home");   die(); } $dir = dirname(__FILE__); $pages = scandir("{$dir}/pages"); unset($pages[0],$pages[1]); foreach ($pages as &$page){   $page = substr($page, 0, strpos($page, '.')); } if(in_array($_GET['page'], $pages)){        $title = $_GET['page'];    $uri = $_SERVER['REQUEST_URI'];    $uri = substr_replace($uri, $title, strrpos($uri, "/") + 1);    $include_page = "{$dir}/pages/{$title}.template.htm";        } else {   header("Location: ?page=home");   die(); } $title = ucwords(str_replace("_", " ", $title)); //everytime this file is accessed, I class it as a page hit. $page_views = file_get_contents("{$dir}/page_views.txt"); $page_views++; file_put_contents("{$dir}/page_views.txt", $page_views); //breadcrumb effect below........ //gets rid of the first / in the directory string $uri = substr_replace($_SERVER['REQUEST_URI'],"",0,strpos($_SERVER['REQUEST_URI'],"?")+1); //explodes string into an array $root_array = explode("&",$uri); if($_GET['page'] == "display") {    array_unshift($root_array, "=products"); } if(in_array("ref=".$_GET['ref'],$root_array)) {       array_pop($root_array); } //counts array length $len = count($root_array); foreach($root_array as $k => &$v) {    if(substr_count($v, "."))    {       $v = substr_replace($v, "", strpos($v, "."));    }       $v = substr($v,strpos($v,"=")+1);    $v = ucwords(str_replace("_"," ",$v,substr_count($v,"_")));    $v = "<span class='bread'>".$v."</span>"; } $crumb = "You are here: ".implode(" -> ",$root_array); ?> So you can see where I set the content to display.  My directory is like so:  <Gardenable>       index.php       <core>             init.inc.php             <pages>                  home.template.htm                  about.template.htm                  products.template.htm                  etc                  etc  I put the .htaccess file in every directory but it had the same effect.  I think it's the actual mod_rewrite statement for sure.  Thank you for your time.  Kind regards,  L2c. Edited August 3, 2013 by Love2c0de Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1443297 Share on other sites More sharing options...
jazzman1 Posted August 3, 2013 Share Posted August 3, 2013 1. Rewrite all URL's from your index.php file <ul> <li><a href="./home">Home</a></li> <li><a href="./about">About</a></li> <li><a href="./products">Products</a></li> <li><a href="./services">Services</a></li> <li><a href="./contact_us">Contact Us</a></li> </ul> Or, with capital letters <ul> <li><a href="./Home">Home</a></li> <li><a href="./About">About</a></li> <li><a href="./Products">Products</a></li> <li><a href="./Services">Services</a></li> <li><a href="./Contact_us">Contact Us</a></li> </ul> 2. Create a .htaccess file in the same directory where the index file is: RewriteEngine on RewriteBase /Gardenable/ RewriteRule ^/?([a-zA-Z_]+)$ index.php?page=$1 [L] Try to run the script again and tell me what resuts you're getting. Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1443303 Share on other sites More sharing options...
jazzman1 Posted August 3, 2013 Share Posted August 3, 2013 Yep, it works as expected. Â Test - http://canada.lesno.net/Gardenable Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1443361 Share on other sites More sharing options...
Love2c0de Posted August 3, 2013 Author Share Posted August 3, 2013 Absolutely fantastic, thank you so much. Â Finally got there in the end. - well nearly! Â How would I now deal with multiple $_GET values in a URL string? Â For example when I click on a link on my products page for example "Planters", it reloads but it shows the URL as http://www.labtec.0fees.net/Gardenable/Products?page=products&order=planters and it doesn't only show the Planters, it shows Planters and Bird Housing still (this is default). Â It's as if my $_GET['order'] value isn't being read. Â Kind regards, Â L2c. Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1443383 Share on other sites More sharing options...
jazzman1 Posted August 4, 2013 Share Posted August 4, 2013 Hey gardener, good for you  Just create a second rule that matches the pattern of your URL, which, you expected to be.  Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1443390 Share on other sites More sharing options...
Love2c0de Posted August 6, 2013 Author Share Posted August 6, 2013 I'll sit down tonight when I get a minute and try researching this. Â It's much better after your help though and it will help SEO with the links which is good. Â Kind regards, Â L2c. Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1443626 Share on other sites More sharing options...
Love2c0de Posted September 1, 2013 Author Share Posted September 1, 2013 (edited) Hi there,  I bought a domain and would like to use the mod_rewriting on this site.  I noticed from previous help hat we used RewriteBase /Gardenable/ as the base url for http://www.labtec.0fees.net/Gardenable/?page=Home  But, on my new domain, it's just http://www.tspv-websites.co.uk/?page=Home  I tried all kinds of values to use as the base url on my new domain and have even taken it out but it's returning an error that it cannot find the specified URL.  Can anyone see what I need to do?  The .htaccess fie is in the root public_html.  Here is the .htaccess file as it stands: RewriteEngine on RewriteRule ^/?([a-zA-Z_]+)$ index.php?page=$1 [L] Thank you for your time.  Kind regards,  L2c. Edited September 1, 2013 by Love2c0de Quote Link to comment https://forums.phpfreaks.com/topic/280578-url/#findComment-1447654 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.