I need help rewriting URLs with Apache Mod_Rewrite.

Discussion in 'Apache' started by mexabet, Oct 13, 2009.

  1. #1
    I need help rewriting the URLs generated dynamically by a PHP photo gallery script.
    The following examples are how the URLs currently show on the browser:

    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").

    I need help to make the above-mentioned "album 1" URL to be: "Tropical_Islands/" and other albums to be in this form.

    The following example is how the image URLs shows:
    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 so far:
    ## 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
    Code (markup):
    Any help will be so much appreciated.
     
    mexabet, Oct 13, 2009 IP
  2. dsignresponder

    dsignresponder Greenhorn

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #2
    First, you'll have to check if your mod_rewrite module is enabled. You can check this by creating a phpinfo.php file at your server's root :
    
    <?
    phpinfo();
    ?>
    
    PHP:
    point your browser to this file and look at the loaded modules.
    NOTE : do NOT store this file too long on your server!!!! After viewing it, print out the output and delete the phpinfo.php!

    Come back then and let us know if your mod_rewrite engine is ON.
     
    dsignresponder, Oct 15, 2009 IP
  3. mexabet

    mexabet Well-Known Member

    Messages:
    866
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #3
    dsignresponder,

    Thanks for your reply. Yes, Mod_Rewrite module is enabled.
     
    mexabet, Oct 15, 2009 IP
  4. dsignresponder

    dsignresponder Greenhorn

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #4
    OK, then try to test it!
    create a test.html :
    
    <h2>This is the HTML file.</h2> 
    
    Code (markup):
    and create a test.php:
    
    
    <h2>This is the PHP file.</h2>
    
    
    PHP:
    Upload them both to your server.
    Create a .htaccess file (you can use Notepad, save as .htaccess, BUT not "filename.htaccess", just ".htaccess") :
    
    RewriteEngine on
    RewriteRule ^/?test\.html$ test.php [L]
    
    Code (markup):
    upload it to your server's root. Point your browser to test.html
    If everything is OK, you should see
    
    This is the PHP file
    
    Code (markup):
    while your browser is showing test.html at the location box.

    If that is working, you should write the rewrite rule! Let me know and I'l try to help you.
    Good luck!
     
    dsignresponder, Oct 15, 2009 IP
  5. mexabet

    mexabet Well-Known Member

    Messages:
    866
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #5
    I know Mod_Rewrite works on my hosting account, because I own other sites there that utilize it. The thing is that I wasn't the person who wrote those ones - the .htaccess files. I know exactly where to place this new one, but writing it is the issue.

    This new htaccess file is meant for a new site I want to add. Coding the right Mod_Rewrite is my problem.

    I do appreciate your time and effort, and seeing me through will be a thing to remember for a long time.

     
    mexabet, Oct 15, 2009 IP
  6. dsignresponder

    dsignresponder Greenhorn

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #6
    dsignresponder, Oct 15, 2009 IP
  7. mexabet

    mexabet Well-Known Member

    Messages:
    866
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #7
    I have been through addedbytes tutorials a cheat sheets several times. I guess I have to settle down and learn regular expressions properly. Thanks for your time!
     
    mexabet, Oct 15, 2009 IP
  8. mexabet

    mexabet Well-Known Member

    Messages:
    866
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #8
    I still can't get this mod_rewrite to work. Can someone please give me a helping hand?

    This is what I have as of now:

    ## 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=$1 [L]
    
    RewriteRule ^([a-zA-Z_]+)/([a-zA-Z_]+)\.html$ index.php?page=image-detail&album=$1&image=$2 [L]
    
    #Options -Indexes IncludesNOEXEC FollowSymLinks
    Code (markup):
    When I try http://www.mydomain.com/Tropical_Islands , only the page Header loads and this error message appears:
    Error, get album name failed. Unknown column 'Tropical_Islands' in 'where clause'
    Code (markup):
     
    mexabet, Oct 18, 2009 IP