Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

This is a discussion on .htaccess question in the Design and Development forum
I'm upgrading an installation of Gallery on my account, and I'd like to set up some .htaccess rules to redirect certain types of links to ...

  1. #1
    Loyal Client
    Join Date
    May 2006
    Posts
    29

    .htaccess question

    I'm upgrading an installation of Gallery on my account, and I'd like to set up some .htaccess rules to redirect certain types of links to another address.

    Gallery gives me some .htaccess code to put transfer links from my old installation (gallery2) to my new installation (gallery3), but it's designed for transferring links from DirectoryA to other links in DirectoryB:

    Code:
    <IfModule mod_rewrite.c>
          Options +FollowSymLinks
          RewriteEngine On
          RewriteBase /gallery2/
          RewriteRule ^(.*)$ /gallery3/index.php/g2/map?path=$1   [QSA,L,R=301]
        </IfModule>
    If I understand this correctly, this .htaccess code will take every link that is directed at /gallery2/ and append that link onto the end of the RewriteRule for the /gallery3/ directory, correct?

    For example, if my gallery2 link is:

    Code:
    http://www.mysite.com/gallery2/main.php?g2_itemId=35
    this will change the link to this:

    Code:
    http://www.mysite.com/gallery3/index.php/g2/map?path=main.php?g2_itemId=35
    correct?

    However, once I'm done with upgrading, I'm going to rename the new /gallery2/ directory to /gallery3/. How would I change this .htaccess so that it directs something to another link in the same directory?

    Every link in Gallery2 is some variation of this:

    http://www.mysite.com/gallery2/main.php?g2_itemId=35

    If I want to keep the same directory, can I change the .htaccess to something like this?

    [code]
    Code:
    <IfModule mod_rewrite.c>
          Options +FollowSymLinks
          RewriteEngine On
          RewriteBase /gallery2/main.php?g2_itemId=
          RewriteRule ^(.*)$ /gallery2/index.php/g2/map?path=main.php?g2_itemId=$1   [QSA,L,R=301]
        </IfModule>
    Thanks for any help, I know this is a little long-winded.

  2. #2
    Loyal Client the_ancient's Avatar
    Join Date
    Feb 2004
    Posts
    3,384
    RewriteBase is a condition to set the Root of the Rewrite URL


    so

    Code:
    <IfModule mod_rewrite.c>
          Options +FollowSymLinks
          RewriteEngine On
          RewriteBase /blog/
          RewriteRule ^(.*)$ /index.php?postid=$1   [QSA,L,R=301]
        </IfModule>
    With that Url "example.com/1"

    would be read by apache as "example.com/blog/index.php=1"

    I will have to look in to Gallery3's Link Structure because

    Code:
    http://www.mysite.com/gallery3/index.php/g2/map?path=main.php?g2_itemId=35
    Is not a Valid URL, you should only have 1 "?" and it should come immediately after index.php then reset of the variables would be separated by "&". Unless they are for some reasoning they are Mixing Get Requests with URL Based Redirected Requests which will make for a htaccess nightmare for you
    -------------------------
    the_ancient
    MP Technology Group

  3. #3
    Loyal Client
    Join Date
    Sep 2011
    Location
    Bellthorpe
    Posts
    214
    This is a very useful .htaccess resource.

  4. #4
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    @TA

    Quote Originally Posted by the_ancient View Post
    With that Url "example.com/1"

    would be read by apache as "example.com/blog/index.php=1"
    Actually, that's not quote right. RewriteBase only sets a base that gets applied to the URL paths that RewriteRules use for comparisons. Without a RewriteBase, the comparison will be from the root of the REQUEST_URI. Typically you would do something like this inside public_html/gallery2/.htaceess:

    Code:
    RewriteBase /gallery2
    RewriteRule ^(.*)$ /gallery3/$1 [R=301,QSA,L]
    Which is analogous to:

    Code:
    RewriteRule ^/gallery2/(.*)$ /gallery3/$1 [R=301,NC,L]
    @Carter

    I'm not sure what you are trying to do in the last section. You say that you are moving your gallery from gallery2 to gallery3, but then you say that you plan to rename the new gallery2 to gallery3. Isn't gallery3 already the "new" directory (as in that's where you're putting the upgrade?

    I don't think there is anything that you need to do. The code you have, from Gallery, already redirects anything going to the old URL (gallery2) to the new one (gallery3), so you can just delete everything in gallery2 except for the .htaccess file and all requests will automagically go to the new version. Or, with a small change, you could move the rewite code to an .htaccess in public_html and remove the gallery2 directory altogether:

    Code:
    RewriteRule ^/gallery2/(.*)$ /gallery3/index.php/g2/map?path=$1   [QSA,L,R=301]
    If you did have an actual need to redirect from one URL structure within gallery2 to a different structure in the same directory, you would need to idenfiy the specific URL patterns that need to be redirected (instead of using ^(.*)$, which literally means "match everything"). Otherwise you would end up with a redirect loop because each time you redirected the new request would match the "redirect everything" rule and redirect again.

    Please correct me if I'm interpreting all of this incorrectly. Mod rewrite is a very powerful tool, but it can become very confusing very quickly and I don't want to overwhelm you with examples that have nothing to do with what you really want to do.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  5. #5
    Loyal Client
    Join Date
    May 2006
    Posts
    29
    Quote Originally Posted by the_ancient
    Is not a Valid URL, you should only have 1 "?" and it should come immediately after index.php then reset of the variables would be separated by "&".
    Ah, now see, that's why I always ask before I mess around with this - thank you. That hadn't occurred to me.

    Quote Originally Posted by jason View Post
    @Carter

    I'm not sure what you are trying to do in the last section. You say that you are moving your gallery from gallery2 to gallery3, but then you say that you plan to rename the new gallery2 to gallery3. Isn't gallery3 already the "new" directory (as in that's where you're putting the upgrade?
    That's a mistake, my bad. I'm basically upgrading my Gallery in the /gallery3/ directory and then renaming it to the old directory name, so everything would take place in the original directory - /gallery2/.

    If you did have an actual need to redirect from one URL structure within gallery2 to a different structure in the same directory, you would need to idenfiy the specific URL patterns that need to be redirected (instead of using ^(.*)$, which literally means "match everything"). Otherwise you would end up with a redirect loop because each time you redirected the new request would match the "redirect everything" rule and redirect again.
    What I'm mostly concerned about is redirecting the links from my message board to the old version of Gallery. The URLs all have the setup like this:

    http://www.mysite.com/gallery2/main.php?g2_itemId=35

    where "35" in that URL changes depending on which page or picture they are trying to match. (For example, another picture might be "http://www.mysite.com/gallery2/main.php?g2_itemId=427".

  6. #6
    Ron
    Ron is offline
    Loyal Client
    Join Date
    Aug 2002
    Posts
    7,304
    Quote Originally Posted by Carter View Post
    What I'm mostly concerned about is redirecting the links from my message board to the old version of Gallery. The URLs all have the setup like this:

    http://www.mysite.com/gallery2/main.php?g2_itemId=35

    where "35" in that URL changes depending on which page or picture they are trying to match. (For example, another picture might be "http://www.mysite.com/gallery2/main.php?g2_itemId=427".
    What exactly will the new link for that item look like?
    Will there be a "main.php" in the new directory, or just index.php?
    Are there other types of links as well that will need to be redirected?

    It probably will be possible to do a "simple" redirect to accomplish your task.
    Good luck

  7. #7
    Loyal Client
    Join Date
    May 2006
    Posts
    29
    Ah yes, I see what you need. Thanks for clarifying.

    The original link would be something like:

    Code:
    http://www.mysite/gallery2/main.php?g2_itemId=37
    The new link would be something like this:

    Code:
    http://www.mysite.com/gallery3/index.php/photos/landscapes
    I suppose since they are so different, you wouldn't really be able to rewrite something like that. That's why I was hoping there was a way to parse the old link to the redirect line in the .htaccess specified here:

    Code:
    RewriteRule ^(.*)$ /gallery3/index.php/g2/map?path=$1   [QSA,L,R=301]

  8. #8
    Ron
    Ron is offline
    Loyal Client
    Join Date
    Aug 2002
    Posts
    7,304
    Quote Originally Posted by Carter View Post
    I suppose since they are so different, you wouldn't really be able to rewrite something like that. That's why I was hoping there was a way to parse the old link to the redirect line in the .htaccess specified here:

    Code:
    RewriteRule ^(.*)$ /gallery3/index.php/g2/map?path=$1   [QSA,L,R=301]
    They will be! Hang on, we'll get it figured out.
    Good luck

  9. #9
    Ron
    Ron is offline
    Loyal Client
    Join Date
    Aug 2002
    Posts
    7,304
    I'm thinking this is what it will look like

    Code:
    RewriteEngine On
    RewriteRule ^main\.php(.*)$ http://www.mysite.com/gallery3/index.php/g2/map?path=main.php$1   [QSA,L,R=301]
    But I need to ponder it just a bit more.... Here's what I'm wondering -- with QSA as an option, the $1 might not even be necessary? Hmmmmm......

    Is this something you can give a quick try, or are you just investigating at this point?
    Good luck

  10. #10
    Loyal Client
    Join Date
    May 2006
    Posts
    29
    Quote Originally Posted by Ron View Post
    Is this something you can give a quick try, or are you just investigating at this point?
    I really appreciate the help, I will try this tonight and get back to you.

  11. #11
    Ron
    Ron is offline
    Loyal Client
    Join Date
    Aug 2002
    Posts
    7,304
    I actually tested this and here are the answers, I think.

    # Exactly as you requested, with a question mark after main.php
    Code:
    RewriteEngine On
    RewriteRule ^main\.php(.*)$ http://www.mysite.com/gallery2/index.php/g2/map?path=main.php?%{QUERY_STRING}   [L,NE,R=301]
    This redirected to
    Code:
    http://www.mysite.com/gallery2/index.php/g2/map?path=main.php?g2_itemId=37




    #As per your provided example, which creates an ampersand after main.php
    Code:
    RewriteEngine On
    RewriteRule ^main\.php(.*)$ http://www.mysite.com/gallery2/index.php/g2/map?path=main.php   [QSA,L,R=301]
    This redirected to
    Code:
    http://www.mysite.com/gallery2/index.php/g2/map?path=main.php&g2_itemId=37
    This .htaccess file goes into the (new) gallery2 directory with permissions of 644
    Remember, this code depends on there not being an actual "main.php" in this directory. If there IS one, there's a little bit more work to do.
    Last edited by Ron; 02-09-2012 at 04:29 PM.
    Good luck

  12. #12
    Loyal Client
    Join Date
    May 2006
    Posts
    29
    The second one worked like a charm. Thank you so much!

    For the record, I'm talking about this one -

    Code:
    RewriteEngine On
    RewriteRule ^main\.php(.*)$ http://www.mysite.com/gallery2/index...?path=main.php   [QSA,L,R=301]
    which makes sense since as was stated above you can't have more than one '?' in a query string. Thanks so much! Will be sure to spread this on to the Gallery Community, since I've seen this request posted elsewhere.

  13. #13
    Ron
    Ron is offline
    Loyal Client
    Join Date
    Aug 2002
    Posts
    7,304
    Eeeexcellent.

    It would be possible to rewrite the queries to the newer seo friendly format as well, but it would require a different approach. An external rewriting proggie could be used or......

    I assume that this link further redirects the browser again to the new SEO friendly link? If so, I'd probably think about doing an internal redirect instead of this external redirect. The internal redirect never gets seen by the user's browser (or the search engine!) and then the output (the final redirect) from the mapping code would be sent. MUCH nicer and cleaner and SEO friendly.

    Assuming my assumption is correct, I think it might work like this:
    Code:
    RewriteEngine On 
    RewriteRule ^main\.php(.*)$ index.php/g2/map?path=main.php   [QSA,L]
    I think that might do it.
    Last edited by Ron; 02-10-2012 at 10:47 PM.
    Good luck

  14. #14
    Loyal Client
    Join Date
    May 2006
    Posts
    29
    That redirect worked really well. Thanks!

    One more request. The old index for gallery2 was at

    mysite.com/gallery2/main.php

    the new index, however is different. It is located at

    mysite.com/gallery2/index.php/

    How would you include a separate redirect to just redirect main.php to index.php/ without running afoul of the other redirect we wrote before?

  15. #15
    JPC Member martin86's Avatar
    Join Date
    Jan 2012
    Posts
    6
    when someone tries to go to a non-existant page the server is going to give a 404 error. So what your htaccess has to do is trap that error code and redirect it to your custom 40 page, or any page you want. vbulletin-smile.gif

Page 1 of 2 12 LastLast

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
  •