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 10-14-2009, 07:44 AM   PM User | #1
mexabet
New Coder

 
Join Date: Sep 2008
Location: Mexabet.biz
Posts: 81
Thanks: 12
Thanked 0 Times in 0 Posts
mexabet is an unknown quantity at this point
I need assistance to rewrite dynamically-generated URLs

I need assistance to rewrite the URLs dynamically generated by a PHP photo gallery script.
These examples are how the URLs currently show on web browsers:

index.php?page=list-image&album=1 (The URL of "album 1" while I name the album "Tropical Islands") and index.php?page=list-image&album=2 (The URL of "album 2 while the name might be something like " Exotic Beaches" or "Beaches").

How can I rewrite the above-mentioned "album 1" URL to be: "Tropical_Islands/" and other dynamically-generated albums to appear in that form.

This is how the image URLs show:
index.php?page=image-detail&album=1&image=1 (The URL of "image 1" of "album 1" while I name this image "Buddha Temple")

I want the image URLs to be in this form: "Tropical_Islands/Buddha_Temple/"

Can anyone please show me how to rewrite the album and image URLs in order to have search engine-friendly URLs. I have tried without success to achieve this. This is what I have done that didn't work:
Code:
## mod_rewrite configuration
Options +FollowSymLinks

# Turn on the rewriting engine
RewriteEngine on

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|png)$ - [F]

RewriteRule ^[a-zA-Z]$ index.php?page=list-image&album=$0
RewriteRule ^([a-zA-Z])\/$ /$1 [R]

RewriteRule ^([A-Z]+)([a-z)]+)([0-9)]+)\.html$ index.php?page=image-detail&album=1&image=$3

RewriteRule  ^([^/]+)/$ index.php?page=image-detail&album=1&image=$1

#Options -Indexes IncludesNOEXEC FollowSymLinks
Any help will be so much appreciated.
mexabet is offline   Reply With Quote
Old 10-14-2009, 01:14 PM   PM User | #2
orca8767
New Coder

 
Join Date: Aug 2009
Posts: 84
Thanks: 1
Thanked 7 Times in 7 Posts
orca8767 can only hope to improve
It is generally better to provide one common Rule and allow a PHP script to parse it with functions like preg_match and such.

Example:
Code:
RewriteEngine On
RewriteBase /

RewriteRule ^(.+)$ parse.php?url=$1

Oh, and I believe the variables for mod rewrite start at $1, not $0.
That, and your second rewrite rule lacks parentheses.
orca8767 is offline   Reply With Quote
Users who have thanked orca8767 for this post:
mexabet (10-14-2009)
Old 10-14-2009, 01:47 PM   PM User | #3
mexabet
New Coder

 
Join Date: Sep 2008
Location: Mexabet.biz
Posts: 81
Thanks: 12
Thanked 0 Times in 0 Posts
mexabet is an unknown quantity at this point
orca8767,

Thanks for your reply and example. I'm new to Apache Mod_Rewrite and I seem not to grasp it clearly. Can you please use my examples/URLs to direct me further?
mexabet is offline   Reply With Quote
Old 10-14-2009, 09:49 PM   PM User | #4
orca8767
New Coder

 
Join Date: Aug 2009
Posts: 84
Thanks: 1
Thanked 7 Times in 7 Posts
orca8767 can only hope to improve
Try using this:

Code:
RewriteEngine On
RewriteBase /

RewriteRule ^([a-zA-Z0-9_])(.html)?$ index.php?page=list-image&album=$1
RewriteRule ^([a-zA-Z0-9_])\/([a-zA-Z0-9_])(.html)?$ index.php?page=image-detail&album=$1&image=$2
This takes /blah.html and /blah all to /index.php?page=list-image&album=blah
And it takes /blah/blah2 and /blah/blah2.html all to /index.php?page=image-detail&album=blah&image=blah2

I added the support for .html in case you decide to use those... Note that it will NOT work if you don't use the .html and you have a trailing slash in a url like http://mysite.com/album/ because if i added this it would wreck indexing of directories and such. Instead of /album/ it should be /album WITHOUT the trailing slash. same with /album/image_name. Or, if you use /album.html and /album/image_name.html you don't even have to worry about making sure you don't include the trailing slashes.
orca8767 is offline   Reply With Quote
Old 10-15-2009, 03:54 AM   PM User | #5
mexabet
New Coder

 
Join Date: Sep 2008
Location: Mexabet.biz
Posts: 81
Thanks: 12
Thanked 0 Times in 0 Posts
mexabet is an unknown quantity at this point
Hi orca8767,

I tried the code, but couldn't get it to work. This is how the whole file looked after adding your code:
Code:
## mod_rewrite configuration
Options +FollowSymLinks

# Turn on the rewriting engine
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|png)$ - [F]

RewriteRule ^([a-zA-Z0-9_])(.html)?$ index.php?page=list-image&album=$1

RewriteRule ^([a-zA-Z0-9_])\/([a-zA-Z0-9_])(.html)?$ index.php?page=image-detail&album=$1&image=$2

#Options -Indexes IncludesNOEXEC FollowSymLinks
mexabet is offline   Reply With Quote
Old 10-15-2009, 12:47 PM   PM User | #6
orca8767
New Coder

 
Join Date: Aug 2009
Posts: 84
Thanks: 1
Thanked 7 Times in 7 Posts
orca8767 can only hope to improve
Can I have more details than "it did not work" please? Like what is the problem? 404 error whenever you tried a URL? 500 error?

Note that it needs to be /album_name or /album_name.html for it to be the album.
And for an image it needs to be /album_name/image_name or /album_name/image_name.html
No trailing slashes if you don't use .html at the end.

EDIT:
I made a simple error in the regular expressions. Stupid nearly-unreadable regular expressions. Grr...

Updated code:
Code:
RewriteRule ^([a-zA-Z0-9_]+)(.html)?$ index.php?page=list-image&album=$1
RewriteRule ^([a-zA-Z0-9_]+)\/([a-zA-Z0-9_]+)(.html)?$ index.php?page=image-detail&album=$1&image=$2

Last edited by orca8767; 10-15-2009 at 12:51 PM..
orca8767 is offline   Reply With Quote
Reply

Bookmarks

Tags
apache mod_rewrite, mod_rewrite, rewrite urls

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 05:53 AM.


Advertisement
Log in to turn off these ads.