1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need help with URL redirection in .htaccess

Discussion in 'Apache' started by mmerlinn, Aug 11, 2019.

  1. #1
    I recently moved links from one directory to another. Rather than doing a Rewrite 301 for each and every link, I would like to do a single Rewrite, but I am clueless on how to make it work.


    Sample OLD URLs:
    SEMrush
    /catalog/flurosenia/hn_4zk/hn4zk130.htm
    /catalog/flurosenia/hn_4zk/hn4zkxcG.html


    Sample NEW URLs:

    /catalog/lily1000125/hn_5rp/hn5rp130.htm
    /catalog/lily1000125/hn_5rp/hn5rpxcG.html

    What I need is something (Rewrite, Redirect, or whatever) that will change the RED sections to the BLUE sections
     
    mmerlinn, Aug 11, 2019 IP
    SEMrush
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,975
    Likes Received:
    1,635
    Best Answers:
    233
    Trophy Points:
    515
    #2
    There's no real way to do it as a single rewrite in just .htaccess if you have a whole bunch of different match conditions -- but yes, if you're doing like a hundred of these that can drag your server performance into the 9th ring of hell as the bigger the .htaccess, the more stuff the server has to process before it can serve non-redirected pages.

    Because of this, the technique I like to use is to redirect 404's to a .PHP that then handles the matches. By intercepting 404's you add no overhead to Apache for matched pages, can style your error messages however you like, AND can handle redirects in PHP by sending 301 instead of 404 using header() for any redirection.

    In your httpd.conf of .htaccess you just add:

    ErrorDocument 404 /404Handler.php

    and then have that .php process the $_SERVER['REQUEST_URI'] to create your 301.

    When possible, try to keep your htaccess tiny. The more regex you have it performing with rewriteCond/rewriteRule, the slower your pages will get. ALL of them, even those not redirected.
     
    deathshadow, Aug 12, 2019 IP
  3. Spoiltdiva

    Spoiltdiva Prominent Member

    Messages:
    6,407
    Likes Received:
    1,848
    Best Answers:
    50
    Trophy Points:
    395
    #3
    The very last thing you need is someone like me who really doesn't know what they are talking about leaving a post here but....at the very least you and @deathshadow can have a good laugh at my expense.:rolleyes:

    O.k. why not just start with:move*.twt destinationDirectory then alphabetically (starting with A) move A*destinationDirectory
     
    Spoiltdiva, Aug 12, 2019 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,975
    Likes Received:
    1,635
    Best Answers:
    233
    Trophy Points:
    515
    #4
    Well, for starters that has nothing to do with the question. Copying/moving the files isn't the issue. He wants to 301 old links -- URI's -- to the new file locations. He's already moved them, moving them is easy. Moving them whilst maintaining only one copy whilst making existing URI's to his page -- such as from Google Search -- automatically redirect to the new URI's is what he's asking how to do.

    He just wants to do so without having dozens if not hundreds of redirects declared in his .htaccess or httpd.conf... since if he had a lot of these:

    
    Redirect 301 /catalog/flurosenia/hn_4zk/hn4zk130.htm /catalog/lily1000125/hn_5rp/hn5rp130.htm
    Redirect 301 /catalog/asdfasdfas/hn_4zk/hn4zk130.htm /catalog/bxcvbxcdvb/hn_5rp/hn5rp130.htm
    Redirect 301 /catalog/xcvbxcvbvcxb/hn_4zk/hn4zk130.htm /catalog/cvnbmvbnm/hn_5rp/hn5rp130.htm
    Redirect 301 /catalog/wertwertwe/hn_4zk/hn4zk130.htm /catalog/tyuityui/hn_5rp/hn5rp130.htm
    
    Code (markup):
    For each and every blasted page/file moved gets pretty tiring, pretty quick... and every one of those will slow down EVERY request, not just the ones being redirected.

    A regex if the replacements are common enough could help reduce it so you're not having to say it for every file, but it's still a slew of rules that will slow Apache down.

    Simply moving the files doesn't preserve the old URI's, and most of the time it's very undesirable to have more than one copy of the file in different locations to preserve the old and the new. Hence the "301 redirect -- moved permanently" HTTP status code.
     
    deathshadow, Aug 13, 2019 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,975
    Likes Received:
    1,635
    Best Answers:
    233
    Trophy Points:
    515
    #5
    Oh, and another reason I prefer hooking the 404 handler for redirects -- on top of it being better performing -- is that not only is pattern matching easier to juggle and understand in languages like PHP -- at least compared to .htaccess/httpd.conf -- it also means that if your coding goes bits-up face-down only the redirect/404's are screwed up.

    You make a mistake in your .htaccess, and it can take the whole site down typically reporting 500 series errors.
     
    deathshadow, Aug 13, 2019 IP
  6. Spoiltdiva

    Spoiltdiva Prominent Member

    Messages:
    6,407
    Likes Received:
    1,848
    Best Answers:
    50
    Trophy Points:
    395
    #6
    It's worse than I thought, not only did I not have the answer...I didn't even fully comprehend the question!:rolleyes: That's it, I do believe I'll stick to what I do know...namely writing.;) In the meantime I'll read your answers and try to keep learning.
     
    Spoiltdiva, Aug 13, 2019 IP
  7. mmerlinn

    mmerlinn Notable Member

    Messages:
    2,261
    Likes Received:
    287
    Best Answers:
    6
    Trophy Points:
    290
    #7
    Well, since there is apparently no easy solution, for the time being I am just going to 301 the critical links and let the rest 404 themselves.
     
    mmerlinn, Aug 15, 2019 IP