Jump to content

URL


Love2c0de

Recommended Posts

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 by jazzman1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by Love2c0de
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by jazzman1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Love2c0de
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 4 weeks later...

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 by Love2c0de
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.