Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-20-2011, 04:09 AM   PM User | #1
jdblewitt
New Coder

 
Join Date: Sep 2006
Posts: 84
Thanks: 47
Thanked 0 Times in 0 Posts
jdblewitt is an unknown quantity at this point
Need help with Mod Rewrite

Hey everyone. I'm not a programmer so I could really use some help to accomplish this....

I want to create urls like this...

/book-clubs/BOOK_CLUB_NAME/authors/AUTHOR_NAME

From this page depending on the query...

/book-clubs/BOOK_CLUB_NAME/authors/index.php?author=AUTHOR_NAME

Each book club directory will have it's own /authors/index.php



The book club name and author name vary, so a real example could be...

/book-clubs/doubleday/authors/james-patterson/index.php

Will be made from...

/book-clubs/doubleday/authors/index.php?author=james-patterson


Also, do I need a separate .htaccess file for each book-club directory?

Thanks!
jdblewitt is offline   Reply With Quote
Old 02-21-2011, 12:33 AM   PM User | #2
bwmarkle
New to the CF scene

 
Join Date: Feb 2011
Location: Virginia Beach
Posts: 5
Thanks: 0
Thanked 3 Times in 3 Posts
bwmarkle is an unknown quantity at this point
Hi jdblewitt,

I started by googling for more information for you. I searched for, "redirect .htaccess fake folders".

I used the information I found on this page to help solve your question:
Create fake subdirectories with htaccess and php AND keep existing directories as is.

I put in place the following in my .htaccess:
Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I then created the index.php with the following code:
PHP Code:
<h1>This is index.php</h1>

<p>You are trying to access: <? echo $_SERVER['REQUEST_URI']; ?><p>

<?
        
// explode the url by /
        
$exploded_url explode("/",$_SERVER['REQUEST_URI']);

        
// get the number of items in the new array
        
$array_size sizeof($exploded_url);

        
// in this URL: book-clubs/doubleday/authors/james-patterson/index.php
        // we know that the author name is the second to last item in the path
        // so that equals $array_size - 1
        
$author_name $exploded_url[$array_size 2];
        
$book_club $exploded_url[$array_size 4];
?>

<p>The author's name is <? echo $author_name?></p>

<p>The book club name is <? echo $book_club?></p>
You can then use the $author_name and $book_club variables to call your database so you can display the correct content.

The above is bare minimum, and works. You can test it here:
http://blog.bradmarkle.com/testing/f...rson/index.php

One .htaccess file is all that you should need.

Let me know if this doesn't help.

Thanks,

- Brad
bwmarkle is offline   Reply With Quote
Users who have thanked bwmarkle for this post:
jdblewitt (02-21-2011)
Old 02-21-2011, 02:38 AM   PM User | #3
jdblewitt
New Coder

 
Join Date: Sep 2006
Posts: 84
Thanks: 47
Thanked 0 Times in 0 Posts
jdblewitt is an unknown quantity at this point
Thank you so much for your time! I'm a little confused about the index page you created. I already have links like this...

/book-clubs/doubleday/authors/index.php?author=author1
/book-clubs/doubleday/authors/index.php?author=author2
/book-clubs/doubleday/authors/index.php?author=author3

I want them to be able to look like this....

/book-clubs/doubleday/authors/author1/index.php
/book-clubs/doubleday/authors/author2/index.php
/book-clubs/doubleday/authors/author3/index.php


Is the htaccess example you provided for this?
jdblewitt is offline   Reply With Quote
Old 02-21-2011, 02:44 AM   PM User | #4
jdblewitt
New Coder

 
Join Date: Sep 2006
Posts: 84
Thanks: 47
Thanked 0 Times in 0 Posts
jdblewitt is an unknown quantity at this point
A real example on my site would be if you go to http://www.bookclubshop.com/book-clu...k-expressions/ and then on the right you will see a menu that says "top authors" You will then see "Carl Weber"

I want to be able to make the

http://www.bookclubshop.com/book-clu...hor=carl-weber

look like

http://www.bookclubshop.com/book-clu...eber/index.php
jdblewitt is offline   Reply With Quote
Old 02-21-2011, 03:04 AM   PM User | #5
bwmarkle
New to the CF scene

 
Join Date: Feb 2011
Location: Virginia Beach
Posts: 5
Thanks: 0
Thanked 3 Times in 3 Posts
bwmarkle is an unknown quantity at this point
Hi jdblewitt,

Thanks for getting back with me.

I looked into this further for you, and after some more googleing I found this site:
5 useful url rewriting examples using .htaccess

The key to success I believe is going to be option 4 for you.

I put this to work and created this .htaccess file:
Code:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ http://www.bookclubshop.com/book-clubs/black-expressions/authors/index.php?author=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ http://www.bookclubshop.com/book-clubs/black-expressions/authors/index.php?author=$1
With this .htaccess file, If I visit:
http://blog.bradmarkle.com/testing/f...y2/carl-webber
or
http://blog.bradmarkle.com/testing/f...2/carl-webber/

... They will be redirected to:
http://www.bookclubshop.com/book-clu...or=carl-webber

If you're concerned about the URL changing, as it does in my test above, I recommend you check out:
.htaccess redirect without changing address bar

Does this help?

Thanks,

- Brad

Last edited by bwmarkle; 02-21-2011 at 03:06 AM..
bwmarkle is offline   Reply With Quote
Users who have thanked bwmarkle for this post:
jdblewitt (02-21-2011)
Old 02-21-2011, 05:11 AM   PM User | #6
jdblewitt
New Coder

 
Join Date: Sep 2006
Posts: 84
Thanks: 47
Thanked 0 Times in 0 Posts
jdblewitt is an unknown quantity at this point
I think we're beating around the right bush at least....

I created the htaccess file with the info you provided but I get a 404 by entering...

http://www.bookclubshop.com/book-clu...rs/carl-weber/

Also, I do wish for the url to stay the same in the address bar (i.e. I want it to look like a static url rather than a dynamic url.
jdblewitt is offline   Reply With Quote
Old 02-21-2011, 05:26 AM   PM User | #7
jdblewitt
New Coder

 
Join Date: Sep 2006
Posts: 84
Thanks: 47
Thanked 0 Times in 0 Posts
jdblewitt is an unknown quantity at this point
Figured it out.....this seems to work so far...

RewriteEngine on
RewriteBase /book-clubs/black-expressions/authors
RewriteRule ^(.*)$ index.php?author=$1 [QSA,L]
jdblewitt is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:51 AM.


Advertisement
Log in to turn off these ads.