...

View Full Version : Need help with Mod Rewrite



jdblewitt
02-20-2011, 05:09 AM
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!

bwmarkle
02-21-2011, 01:33 AM
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. (http://stackoverflow.com/questions/2766819/create-fake-subdirectories-with-htaccess-and-php-and-keep-existing-directories-as)

I put in place the following in my .htaccess:

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:

<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/fake_directory/book-clubs/doubleday/authors/james-patterson/index.php

One .htaccess file is all that you should need.

Let me know if this doesn't help.

Thanks,

- Brad

jdblewitt
02-21-2011, 03:38 AM
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
02-21-2011, 03:44 AM
A real example on my site would be if you go to http://www.bookclubshop.com/book-clubs/black-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-clubs/black-expressions/authors/index.php?author=carl-weber

look like

http://www.bookclubshop.com/book-clubs/black-expressions/authors/carl-weber/index.php

bwmarkle
02-21-2011, 04:04 AM
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 (http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html)

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

I put this to work and created this .htaccess file:

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/fake_directory2/carl-webber
or
http://blog.bradmarkle.com/testing/fake_directory2/carl-webber/

... They will be redirected to:
http://www.bookclubshop.com/book-clubs/black-expressions/authors/index.php?author=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 (http://stackoverflow.com/questions/1304492/htaccess-redirect-without-changing-address-bar)

Does this help?

Thanks,

- Brad

jdblewitt
02-21-2011, 06:11 AM
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-clubs/black-expressions/authors/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
02-21-2011, 06:26 AM
Figured it out.....this seems to work so far...

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum