Jump to content


Photo

URL


  • Please log in to reply
25 replies to this topic

#1 Love2c0de

Love2c0de

    Advanced Member

  • Members
  • PipPipPip
  • 366 posts
  • LocationThe Pleiades
  • Age:23

Posted 27 July 2013 - 05:03 PM

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/Home

Kind regards,

L2c

Edited by Love2c0de, 27 July 2013 - 05:04 PM.


#2 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,660 posts
  • LocationMississauga, Canada

Posted 27 July 2013 - 05:15 PM

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, 27 July 2013 - 05:17 PM.


#3 Love2c0de

Love2c0de

    Advanced Member

  • Members
  • PipPipPip
  • 366 posts
  • LocationThe Pleiades
  • Age:23

Posted 27 July 2013 - 05:52 PM

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



#4 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,660 posts
  • LocationMississauga, Canada

Posted 27 July 2013 - 09:13 PM

Hey @Love2c0de,

take a look at this good article, especially the part contains "mod-rewrite with Regular Expressions".

http://www.sitepoint...write-examples/

jazz.



#5 Love2c0de

Love2c0de

    Advanced Member

  • Members
  • PipPipPip
  • 366 posts
  • LocationThe Pleiades
  • Age:23

Posted 28 July 2013 - 03:11 AM

I'll give it a read now. 

 

Thanks for the information.

 

Kind regards,

 

L2c.



#6 Love2c0de

Love2c0de

    Advanced Member

  • Members
  • PipPipPip
  • 366 posts
  • LocationThe Pleiades
  • Age:23

Posted 28 July 2013 - 03:42 AM

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, 28 July 2013 - 03:46 AM.


#7 Love2c0de

Love2c0de

    Advanced Member

  • Members
  • PipPipPip
  • 366 posts
  • LocationThe Pleiades
  • Age:23

Posted 28 July 2013 - 04:36 AM

When moving my database and tables to another host, do I just export it and then import to the new host?

 

Kind regards,

 

L2c.



#8 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,660 posts
  • LocationMississauga, Canada

Posted 28 July 2013 - 06:39 AM

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.



#9 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,660 posts
  • LocationMississauga, Canada

Posted 28 July 2013 - 12:22 PM

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...ornia/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....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.

 



#10 Love2c0de

Love2c0de

    Advanced Member

  • Members
  • PipPipPip
  • 366 posts
  • LocationThe Pleiades
  • Age:23

Posted 29 July 2013 - 12:27 PM

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.



#11 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,660 posts
  • LocationMississauga, Canada

Posted 29 July 2013 - 01:29 PM

Ask them!



#12 Love2c0de

Love2c0de

    Advanced Member

  • Members
  • PipPipPip
  • 366 posts
  • LocationThe Pleiades
  • Age:23

Posted 29 July 2013 - 04:24 PM

^^ That could be a good idea... :happy-04:

 

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.



#13 Love2c0de

Love2c0de

    Advanced Member

  • Members
  • PipPipPip
  • 366 posts
  • LocationThe Pleiades
  • Age:23

Posted 29 July 2013 - 04:54 PM

Awesome, it is enabled.

 

When I add your code to the .htaccess file in htdocs, the string in the URL is: http://labtec.0fees..../home?page=home

 

Any thoughts? :suicide:

 

Kind regards,

 

L2c


Edited by Love2c0de, 29 July 2013 - 05:01 PM.


#14 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,660 posts
  • LocationMississauga, Canada

Posted 30 July 2013 - 06:46 AM

The link of your home page 'http://www.labtec.0f...able/?page=home' is equivalent "http://www.labtec.0f....php?page=home"

So if you want this link to be much more pretty something like - 'http://www.labtec.0f...ardenable/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/Gar...home?page=home'


Edited by jazzman1, 30 July 2013 - 06:47 AM.


#15 Love2c0de

Love2c0de

    Advanced Member

  • Members
  • PipPipPip
  • 366 posts
  • LocationThe Pleiades
  • Age:23

Posted 30 July 2013 - 03:42 PM

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.



#16 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,660 posts
  • LocationMississauga, Canada

Posted 31 July 2013 - 06:20 AM

You are doing something wrong.

 

The regular expression is just a pattern that describes a set of strings, so you can compensate that ;)



#17 Love2c0de

Love2c0de

    Advanced Member

  • Members
  • PipPipPip
  • 366 posts
  • LocationThe Pleiades
  • Age:23

Posted 02 August 2013 - 03:08 AM

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.



#18 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,660 posts
  • LocationMississauga, Canada

Posted 02 August 2013 - 10:33 AM

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.



#19 Love2c0de

Love2c0de

    Advanced Member

  • Members
  • PipPipPip
  • 366 posts
  • LocationThe Pleiades
  • Age:23

Posted 02 August 2013 - 07:49 PM

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;">&#169;</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, 02 August 2013 - 07:53 PM.


#20 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,660 posts
  • LocationMississauga, Canada

Posted 02 August 2013 - 08:35 PM

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com