SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2010
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I rewrire this link?

    Hi Everyone..

    Hope you can help me with this...
    I would like to rewrite this link

    http://www.rajeevthomas.com/viewgall...?cid=6&pid=392

    to this

    http://www.rajeevthomas.com/viewgallery/6/392

    Do I rewrite that like this?

    RewriteRule ^viewgallery/([0-9]+)/([A-Za-z0-9-]+)?$ viewgallery.php?cid=$1&pid=$392 [NC,L]

    Am I doing this right?

    Thanks for any help..

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    Hi yath!

    You were close ... see edits of your code below:

    Quote Originally Posted by yathrakaaran View Post
    Hi Everyone..

    Hope you can help me with this...
    I would like to rewrite this link

    http://www.rajeevthomas.com/viewgall...?cid=6&pid=392

    to this

    http://www.rajeevthomas.com/viewgallery/6/392

    Do I rewrite that like this?

    RewriteRule ^viewgallery/([0-9]+)/([A-Za-z0-9-]+)?$ viewgallery.php?cid=$1&pid=$392 [NC,L]

    Am I doing this right?

    Thanks for any help..
    Comments:

    1. Be sure that you have Options -MultiViews in your .htaccess file. If not, MultiViews will allow the viewgallery directory to be hijacked to the viewgallery.php script and you will not like the consequences.

    2. Your example required that the cid value be supplied (as digit(s)) so I eliminated the letters and the ? which made the cid value optional.

    3. Because the URI is case sensitive, you do NOT want to use the No Case flag!

    You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It's helped may members and should help you, too.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2010
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dklynn... Thank you for your answer. Every word you said is a learning experience!. I have to look them up quite a bit! After going through your tutorials I realized that I need to use RewriteCond and use redirect in my links. I am not sure how to do it quite yet. But I tried to do it..can you please help me with it...

    Currently my link structure is this...

    xyz.com/viewgallery.php?cname=Colorado-Fall&pcaption=Touched-By-Light

    But I would like the viewer to see this link xyz.com/viewgallery/Colorado-Fall/Touched-By-Light ( This means redirect?)

    Then rewrite back to my link. ( ?? This is how I understand it)...

    So I wrote...

    # check if the actual request if for "this1"

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /viewgallery.php\?cname=(.*)&pcaption=(.+)

    # redirect to "this2"
    RewriteRule ^viewgallery\.php /%1/%2/?%3 [R=301,L]

    # now rewrite "this2" back to "this1"
    RewriteRule ^(.*?)/(.+)$ /viewgallery.php?cname=$1&pcaption=$2 [L,QSA]


    Is this correct? I am sure it looks like gibberish ..sorry about that!...

    Thanks for your help..

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2010
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I finally got this ...I think this is the right one but it is still not working... is makes the large image itself disappear...

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(viewgallery)\.php\?cname=([^&]+)&pcaption=([^&\s]+) [NC]
    RewriteRule ^ /%1/%2/%3? [L,R=301,NE]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?cname=$2&pcaption=$3 [L,QSA]
    What am I doing wrong?




    Quote Originally Posted by yathrakaaran View Post
    dklynn... Thank you for your answer. Every word you said is a learning experience!. I have to look them up quite a bit! After going through your tutorials I realized that I need to use RewriteCond and use redirect in my links. I am not sure how to do it quite yet. But I tried to do it..can you please help me with it...

    Currently my link structure is this...

    xyz.com/viewgallery.php?cname=Colorado-Fall&pcaption=Touched-By-Light

    But I would like the viewer to see this link xyz.com/viewgallery/Colorado-Fall/Touched-By-Light ( This means redirect?)

    Then rewrite back to my link. ( ?? This is how I understand it)...

    So I wrote...

    # check if the actual request if for "this1"

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /viewgallery.php\?cname=(.*)&pcaption=(.+)

    # redirect to "this2"
    RewriteRule ^viewgallery\.php /%1/%2/?%3 [R=301,L]

    # now rewrite "this2" back to "this1"
    RewriteRule ^(.*?)/(.+)$ /viewgallery.php?cname=$1&pcaption=$2 [L,QSA]


    Is this correct? I am sure it looks like gibberish ..sorry about that!...

    Thanks for your help..

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    Hi yath!

    Since I'm not familiar with either your design or your requirements, I can only comment on your code (and request that you use the [code] ... [/code] wrapper for your code to prevent it from disappearing from the quoted response):

    Code:
    Options +FollowSymLinks -MultiViews 
    # FollowSymLinks should already be in the httpd.conf, i.e., superfluous
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    # This is dangerous but only because it can redirect to the DocumentRoot without your intending to do so
    
    # redirect ORIGINAL request FROM the serveable version to the /'d version
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(viewgallery)\.php\?cname=([^&]+)&pcaption=([^&\s]+) [NC]
    RewriteRule ^ /%1/%2/%3? [L,R=301,NE]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?cname=$2&pcaption=$3 [L,QSA]
    # / is not necessary and could cause problems
    # BECAUSE Apache will first go to the server root to look for $2/$3 then your DocumentRoot
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2010
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    Hi yath!

    Since I'm not familiar with either your design or your requirements, I can only comment on your code (and request that you use the [code] ... [/code] wrapper for your code to prevent it from disappearing from the quoted response):

    Code:
    Options +FollowSymLinks -MultiViews 
    # FollowSymLinks should already be in the httpd.conf, i.e., superfluous
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    # This is dangerous but only because it can redirect to the DocumentRoot without your intending to do so
    
    # redirect ORIGINAL request FROM the serveable version to the /'d version
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(viewgallery)\.php\?cname=([^&]+)&pcaption=([^&\s]+) [NC]
    RewriteRule ^ /%1/%2/%3? [L,R=301,NE]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?cname=$2&pcaption=$3 [L,QSA]
    # / is not necessary and could cause problems
    # BECAUSE Apache will first go to the server root to look for $2/$3 then your DocumentRoot
    Regards,

    DK
    Thank you DK.....thank you for explaining everything. I have a question... do pretty URLs work only with absolute paths to images and directories?

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by yathrakaaran View Post
    Thank you DK.....thank you for explaining everything. I have a question... do pretty URLs work only with absolute paths to images and directories?
    Hi yath!

    If your mod_rewrite code is changing the directory level (as seen by the visitors' browsers - this is what you're doing), then the answer is yes, otherwise, it's not necessary.

    For more information, I have a section of my signature's tutorial Article entitled "Relative Links are Missing!" which should explain it better than the above sentence.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

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