Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2008
    Location
    Mexabet.biz
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    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.

  • #2
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    1
    Thanked 7 Times in 7 Posts
    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.

  • Users who have thanked orca8767 for this post:

    mexabet (10-14-2009)

  • #3
    New Coder
    Join Date
    Sep 2008
    Location
    Mexabet.biz
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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?

  • #4
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    1
    Thanked 7 Times in 7 Posts
    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.

  • #5
    New Coder
    Join Date
    Sep 2008
    Location
    Mexabet.biz
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts
    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

  • #6
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    1
    Thanked 7 Times in 7 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

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