[RESOLVED] Rewriterules for different folder levels
Page 1 of 3 123 LastLast
Results 1 to 15 of 36

Thread: [RESOLVED] Rewriterules for different folder levels

  1. #1
    Somewhere on Mars chrisguk's Avatar
    Join Date
    Jan 2010
    Location
    Someone tell me?
    Posts
    208

    resolved [RESOLVED] Rewriterules for different folder levels

    Hi,

    Firstly I apologise that this may not strictly be a PHP question. But if you can assist then great.

    After some great assistance from a friend I have got to a stage where im a little stuck. Firstly in plain English I will try to explain the result im trying to achieve:

    NB. Please feel free to advise if my structure is incorrect.

    A user is sat on my homepage and they see in the menu bar "About us", in the status bar the URL is displayed as follows:

    http://www.example.com/about

    The content file for the page about is called about.php and is located in the webroot above public_html (I have this part working with the following rule)

    Code:
    RewriteRule ^(([^/]+/)*[^/.]+)$ index.php?page=$1 [L]
    While navigating the "About us" page the user sees a link to view someones profile called "Profiles", as they hover over that link in the status bar the URL is displayed as follows because I wanted profiles to be a child of about:

    http://www.example.com/about/profiles

    Presently I have been unable to achieve this because of mainly a lack of knowledge when it comes to regex etc.

    In terms of my folder structure on the server what would you recomend? is it necessary to place all of the files that are "Child files" to the about.php page in a folder called about, or, does the name of the folder not matter?

    I am aware that if you have a folder with the same name as the parent file = "about.php" then problems occur, or at least they do for me.

    The closest I got was using the following rule but as g1smd quite rightly advised I shouldnt use the optional / otherwise it can lead to duplicate content:

    Profiles.php is now located in a folder called "us"

    About.php is in the webroot

    Code:
    RewriteRule ^us/?(([^/]+/)*[^/.]+)?$ /us/index.php?page=$1 [L]
    I believe this approach is completely wrong about as the URL would be displayed as:

    http://www.example.com/us/profiles

    Instead of what I want to achieve:

    http://www.example.com/about/profiles (And all the other files located in the same folder like below)
    http://www.example.com/about/anotherprofiles
    http://www.example.com/about/biggerprofiles

    For those kind people that can help me here, would you be kind enough to give advice on the structure of my link too.

    Many thanks in advance
    Try not to become a man of success but a man of value

  2. #2
    Senior Member
    Join Date
    Aug 2002
    Location
    Norway
    Posts
    151
    Are you using a framework?
    have a nice OO day!

  3. #3
    Somewhere on Mars chrisguk's Avatar
    Join Date
    Jan 2010
    Location
    Someone tell me?
    Posts
    208
    Quote Originally Posted by gammaster View Post
    Are you using a framework?
    Hi,

    No im just using my own code and design
    Try not to become a man of success but a man of value

  4. #4
    Senior Member
    Join Date
    Aug 2002
    Location
    Norway
    Posts
    151
    If you are using plain files for each page I dont understand why use rewrite rule at all.

    Why not just link to www.domain.com/about.php or have a directory named about and put a index.php in there?

    The content file for the page about is called about.php and is located in the webroot above public_html
    Is the webroot above public_html?

    And I dont understand that profil stuff under "about", but maybe there is something with your environment i dont see.
    have a nice OO day!

  5. #5
    Somewhere on Mars chrisguk's Avatar
    Join Date
    Jan 2010
    Location
    Someone tell me?
    Posts
    208
    Quote Originally Posted by gammaster View Post
    If you are using plain files for each page I dont understand why use rewrite rule at all.

    Why not just link to www.domain.com/about.php or have a directory named about and put a index.php in there?


    Is the webroot above public_html?

    And I dont understand that profil stuff under "about", but maybe there is something with your environment i dont see.
    Index.php if using a GET method to pull the content pages in, so normally the URL would look like this:

    http://www.phpbuilder.com/board/newr...ply&p=11002187

    For example instead of this:

    http://www.phpbuilder.com/board/newreply/11002187
    Try not to become a man of success but a man of value

  6. #6
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,349
    Quote Originally Posted by chrisguk View Post
    Index.php if using a GET method to pull the content pages in, so normally the URL would look like this:

    http://www.phpbuilder.com/board/newr...ply&p=11002187

    For example instead of this:

    http://www.phpbuilder.com/board/newreply/11002187
    Right... and what do you gain by doing all this work to go from the former example to the latter?

  7. #7
    Un Re Member cretaceous's Avatar
    Join Date
    Sep 2004
    Location
    London UK
    Posts
    934
    It would simplify your life if you direct all urls to a single file and then show the content based on the url
    This would work for static or dynamic content from a database

    You can use htacces mod_rewrites for all that and then you don't need directories to match an arbitrary page structure
    - it's not simple to know what you need to change for the rewrite, and you will need to change a fair bit of your code structure, but you can add a different rule depending on number of levels in the url

  8. #8
    Somewhere on Mars chrisguk's Avatar
    Join Date
    Jan 2010
    Location
    Someone tell me?
    Posts
    208
    Guys,

    Im a little confused by your replies.

    All I am trying to achieve is to have friendly URLs across my site. I have many rules in my .htaccess file which server other purposes and in fairness wasnt much work.

    I have had friendly URLs working perfect when the output was like this:

    http://www.example.com/about

    but when I decided to expand the content and have child pages of about for example, I hit a brick wall.

    I read everywhere that search engine bots like friendly URLs and they are not big fans of index.php?=p etc etc. Also the people that visit my site would find it easy to remember where they navigated last.

    It would simplify your life if you direct all urls to a single file and then show the content based on the url
    This would work for static or dynamic content from a database

    You can use htacces mod_rewrites for all that and then you don't need directories to match an arbitrary page structure
    - it's not simple to know what you need to change for the rewrite, and you will need to change a fair bit of your code structure, but you can add a different rule depending on number of levels in the url
    @cretaceous

    Im quite intrigued by your post and wondered if you are able to explain further. Im trying to learn PHP still and anything you provide will help my knowledge for the future, even to help others on this forum
    Try not to become a man of success but a man of value

  9. #9
    Senior Member
    Join Date
    Aug 2002
    Location
    Norway
    Posts
    151
    Quote Originally Posted by cretaceous View Post
    It would simplify your life if you direct all urls to a single file and then show the content based on the url
    This would work for static or dynamic content from a database

    You can use htacces mod_rewrites for all that and then you don't need directories to match an arbitrary page structure
    - it's not simple to know what you need to change for the rewrite, and you will need to change a fair bit of your code structure, but you can add a different rule depending on number of levels in the url
    If he want to have a front controller that takes all the requests thats ok, but he hasnt. So why make some odd get variable to get a plain file like filename.php?
    have a nice OO day!

  10. #10
    Somewhere on Mars chrisguk's Avatar
    Join Date
    Jan 2010
    Location
    Someone tell me?
    Posts
    208
    So far I have not really said how things function on my site so sorry for that.

    Index.php has various pieces of code which include arrays that provide content to individual pages. So towards the bottom of my code I have a header include, a content page include based on the $page; variable and then the footer include. So all the content pages are pulled into index.php
    Try not to become a man of success but a man of value

  11. #11
    Senior Member
    Join Date
    Aug 2002
    Location
    Norway
    Posts
    151
    Ok! I misunderstood and you have a kind of front controller, but...

    I have never designed sites like this and, as an example, if you have a page like "about" why have an index.php like:

    PHP Code:
    include 'header.php';
    include 
    'about.php';
    include 
    'footer.php'
    instead of have a direct callable file about.php like:
    PHP Code:
    include 'header.php';

    // Here comes all the about stuff.

    include 'footer.php'
    have a nice OO day!

  12. #12
    Somewhere on Mars chrisguk's Avatar
    Join Date
    Jan 2010
    Location
    Someone tell me?
    Posts
    208
    Let's just say I have it designed like that because I don't know any better

    I guess a lot of PHP newbies go through this weird design problem.

    You mentioned you have never designed site like this, are you able to give an example on how you would do things?
    Try not to become a man of success but a man of value

  13. #13
    Senior Member
    Join Date
    Aug 2002
    Location
    Norway
    Posts
    151
    Quote Originally Posted by chrisguk View Post
    Let's just say I have it designed like that because I don't know any better

    I guess a lot of PHP newbies go through this weird design problem.

    You mentioned you have never designed site like this, are you able to give an example on how you would do things?
    If I have a page with let say an about page, a contact page, a link page and a forum page i have done it like this and without any rewriting at all if i had a physically file for each of them.

    www.domain.com
    www.domain.com/about.php
    www.domain.com/contact.php
    www.domain.com/links.php
    www.domain.com/forum.php?thread=210

    If you want a front controller that takes all your requests. Drop the get variables and use a .htaccess file like this instead and split up the uri to rout it right.

    It says. If the requested file dont exists let the index.php handle it.
    Code:
    RewriteEngine on
    
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # otherwise forward it to index.php
    RewriteRule . index.php
    In index.php you...
    PHP Code:
    $uriChunks explode('/'$_SERVER['REQUEST_URI']); 
    And get the right routes.
    have a nice OO day!

  14. #14
    Un Re Member cretaceous's Avatar
    Join Date
    Sep 2004
    Location
    London UK
    Posts
    934
    Yes I agree with the last post of Gammaster - from your current structure description it seems that you are not far from getting there

    you can either explode the url, like he shows, to find out what each part is - or you could do it in the mod_rewrite

    either way you should be able to get the page name being looked for

    (I'm not sure what correct term is for the chunks in a url between the slashes - for now I'll call it url component)

    So using mod rewrite, if you name one of the url components to equal urlitem1
    $foo =$_GET['urlitem1'];

    or if you use the exploded result:
    $foo =$uriChunks[1];

    (though clearly the number of depths of the url can move this about - array_reverse can help here)

    so if $foo=='about' you then do an include on that, or hit the database etc

    and you need to make sure falsely named pages are dealt with nicely etc

    hth!

  15. #15
    Somewhere on Mars chrisguk's Avatar
    Join Date
    Jan 2010
    Location
    Someone tell me?
    Posts
    208
    I feel kind of lost now or that I have hit a brick wall. At one point I thought my site development was going well, but after the comments above I have been stopped in my stride and Im not sure to start my project again.

    I tried to explain to "cretaceous" that the way he and "gammaster" create their sites is completely new to me and I have no idea where to start.

    Im not one of these people that will just chuck anything together and say "bahh that will do". I want to do it right and from the outset.

    If you guys are willing to educate me I am willing to learn and hopefully share some good decent content with the other forums users.

    I want to pull all my request from the index.php file, what do I put in this?

    index.php
    PHP Code:
    /*  Content of my index.php */ 
    I understand that I will need a .htaccess, what is your recomendation for its content?

    .htaccess
    Code:
    //Content of my .htccess
    
    //Suggested code was 
    
    RewriteEngine on
    
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # otherwise forward it to index.php
    RewriteRule . index.php
    I have the following files/folders in my web folder:

    home.php

    about.php
    about/profiles.php
    about/history.php

    contact.php
    contact/find-us.php

    flowers.php
    flowers/red-ones.php
    flowers/blue-ones.php
    flowers/blue-ones/small-blue-ones.php

    includes/header.php
    includes/footer.php

    This is what my original index.php looked like:

    PHP Code:
    <?php
    $page 
    = isset($_GET['p']) ? $_GET['p'] : 'about';

    switch(
    $page) {
    /*----------------------- PAGES -----------------------------------*/

    case 'about':
    $title 'My Title';
    $keyword 'A few keywords';
    $description 'A good description.';
    break;

    case 
    'about/profiles':
    $title 'My Title';
    $keyword 'A few keywords';
    $description 'A good description.';
    break;

    case 
    'about/history':
    $title 'My Title';
    $keyword 'A few keywords';
    $description 'A good description.';
    break;

    //So on and so on, you get the gist!

    default:
    $title 'My Title';
    $keyword 'A few keywords';
    $description 'A good description.';
    break;
    }
    include(
    $_SERVER['DOCUMENT_ROOT']. '/include/header.php');  

    include(
    $_SERVER['DOCUMENT_ROOT']. '/'.$page.'.php');

    include(
    $_SERVER['DOCUMENT_ROOT']. "/include/footer.php");

    ?>
    Try not to become a man of success but a man of value

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •