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.

How to do 301’s with different keywords in new URLs and redirect www.’s AND non www http to https?

Discussion in 'Apache' started by Pete, Aug 7, 2019.

  1. #1
    I’m moving a 180 page html non https site with http://www and the non www version over to WordPress and to https AND changing the keywords of the URLs in an .htaccess file in Apache

    Some of the files are http: //www.site.com/keyword-abc.html

    AND sadly some are duplicates of those files but not www. I.e., http: //site.com/keyword-abc.html

    So I need to manually find some way to list both the html files

    http: //www.site.com/keyword-abc.html

    And the
    SEMrush
    http: //site.com/keyword-abc.html

    And redirect it not only to https but also change the URL after the domain name like

    https: //site.com/new-keyword-that-ranks/

    I don’t want to use a WordPress plugin to do it but .htaccess file in Apache.

    I know I will have to write a line of code or two for each URL I want to change.

    How to I write the code for moving both the www and the non www http urls to https and totally different keywords in the htaccess file and not have chains of redirects?

    Does it matter if I do the http: //www.site.com/keyword-abc.html first before the http: //site.com/keyword-abc.html in the redirect?

    If I put a space between each URL change to be able to see and trouble shoot it easier, will that affect the speed of the redirects or cause any problems?

    I’ve gone over lots of tutorials for .htaccess and have never seen any of them that does both a www non secure to https redirect, AND an http:// no www to https redirect AND different keywords in the new URL after the domain name

    I think the hard part is the different keywords after the domain name but I've already built the site with those URLs in it.

    Thanks
     
    Pete, Aug 7, 2019 IP
    SEMrush
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,894
    Likes Received:
    1,616
    Best Answers:
    231
    Trophy Points:
    515
    #2
    When it comes to forcing www to non-www and http to https, I use this:

    
    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
    
    Code (markup):
    It's nice as it short-circuits itself into redirect for "not HTTPS", then does the host matching to turn "www" into non-www., all with a properly formed 301. Even better, it's not site specific, not wasting time matching the actual domain name so everything else is preserved.

    Far too many examples I come across have you typing your actual domain into it, instead of leveraging the regex to properly isolate the individual pieces-parts.

    As to doing 301 for the rest of your domain, that would require a different set of regex rules that would, well... get a bit complex and drag apache's performance down BADLY. The more rules you place in .htaccess or httpd.conf, the more overhead you add to EVERY file request, not just the ones that are invalid/redirected.

    To that end -- as I just told someone else -- my preferred place to put redirects is in the 404 handler. If you set up a custom errorDocument you can use PHP or some other language to run your keyword checks there, using header() to force a 301 if needed, otherwise showing the normal 404.

    I would NOT even try to put all the possible unique redirects into the .htaccess, the overhead penalty is just too high on normal / well formed / proper requests.
     
    deathshadow, Aug 13, 2019 IP
  3. purdue512

    purdue512 Well-Known Member

    Messages:
    858
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #3
    We did this in AWS, with rules in the Load Balancer (Elastic Load Balancing). We like to have the https off-loaded from the web server.
     
    purdue512, Aug 13, 2019 IP
  4. Pete

    Pete Well-Known Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    116
    #4
    Thanks Deathshadow,

    But I want it in the 301 not the 404. I've got 180 pages some http://example.com and some http://www.example.com so maybe only 360 individual html total urls vs thousands.

    Would this code work? Will post it in the reply below
     
    Pete, Aug 16, 2019 IP
  5. Pete

    Pete Well-Known Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    116
    #5
    # 301 Redirect 1
    RewriteRule ^addintheworkplacearticles\.html$ https://example.com/manage-adhd-at-work/ [R=301,L]

    # 301 Redirect 2
    RewriteRule ^adhdmedicationart\.html$ https://example.com/adhd-medication-articles/ [R=301,L]

    # 301 Redirect 3
    RewriteRule ^adhdmedicationco\.html$ https://example.com/adhd-medication-company-websites/ [R=301,L]

    # continue this on all the other individual page to page redirects, then paste this code after it

    # Redirect non-HTTP or non-www to HTTPS without www
    RewriteCond %{HTTPS} =off [OR]
    RewriteCond %{HTTP_HOST} !^(example\.com)$?
    RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

    than after that code, put the WordPress htaccess code
     
    Pete, Aug 16, 2019 IP
  6. mmerlinn

    mmerlinn Notable Member

    Messages:
    2,239
    Likes Received:
    282
    Best Answers:
    6
    Trophy Points:
    260
    #6
    Pete, @deathshadow was NOT telling you to 404 your links, only to 301 them in a way that won't slow down your site with hundreds of redirect 301s in .htaccess.


    As to whether your code will work , put the redirects one at a time in .htaccess and see if they work or not.
     
    mmerlinn, Aug 17, 2019 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,894
    Likes Received:
    1,616
    Best Answers:
    231
    Trophy Points:
    515
    #7
    That's still enough to slow EVERY page of your site regardless of if the redirect applies, because even on completable URI's that .htaccess code has to run.

    Work, yes. Be a good idea? NO! Even 180 of them would result in massive increase in response times for ALL file requests. It's one of the fastest ways to slow down a website.

    As @mmerlinn said, I wasn't telling you to 404 them, I was saying make a 404 handler that outputs a proper 301.

    That would reduce the .htaccess code to:

    ErrorDocument 404 /custom404handler.php
    Code (markup):
    For ALL your redirects. Then you would have a custom404handler.php that would read something like:

    
    <?php
    
    $redirects = [
    	'/oldwhereever1/oldwhatever1.html' => '/newwhereever1/newwhatever1.html',
    	'/oldwhereever2/oldwhatever2.html' => '/newwhereever2/newwhatever2.html',
    	'/oldwhereever3/oldwhatever3.html' => '/newwhereever3/newwhatever3.html',
    	'/oldwhereever4/oldwhatever4.html' => '/newwhereever3/newwhatever4.html',
    ];
    
    if (array_key_exists($_SERVER['REQUEST_URI'], $redirects) {
    	header('HTTP/1.1 301 Moved Permanently'); 
    	header('Location: ' . $redirects[$_SERVER['REQUEST_URI']]);
    } else {
    	header('HTTP/1.0 404 Not Found');
    	echo '404, Requested page not found';
    	// or whatever you want your custom 404 to look like
    }
    
    Code (markup):
    The header() is what sets the HTTP response code, NOT Apache. As such if your redirect has a match, you can 301 it in the 404 handler, making the 404 handler no longer 404 in those cases.

    ... and more complex matches are easier to hash out in languages like PHP or node.js than they are in Apache instructions. You could easily split out the parts of $_SERVER['REQUEST_URI'] for things like the path, filename, extension, etc.

    This code only running in the case of a 404 outputting EITHER a 404 or 301, instead of running each and every time, even on valid file requests.
     
    deathshadow, Aug 17, 2019 IP
  8. Pete

    Pete Well-Known Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    116
    #8
    Pete, Aug 18, 2019 IP
  9. Pete

    Pete Well-Known Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    116
    #9
    Thanks deathshadow.

    I’ve never done 301’s before but have a health dose of caution on all the warnings that doing it wrong can wreck your site.

    Strange I went over dozens of article on 301’s and http to https including 30-40 page comprehensive ones and never heard of that 404 trick. Then went to search it and did find a few others doing it.

    Very smart idea.

    Why bother doing a regular 301 in htaccess or a plugin like redirection when you can offload all the bandwidth and speed hits in a 404?

    So I’m using Thrive themes for my WordPress blog. It has a 404Page Page Template code. (404-page.php) and also a 404 Template code (404.php).

    Do I keep delete both and create a new custom404handler.php file? Or keep one or both and create a new custom404handler.php file?

    I don’t know php code, is this the correct code?

    In the .htaccess file I’d post this
    # force www to non-www and http to https
    
    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
    
    Code (markup):
    Then this in the same .htaccess file I'd do this

    # move errors to custom404handler.php
    
    ErrorDocument 404 /custom404handler.php
    Code (markup):
    Then create a custom404handler.php page in my theme folder, and paste this code in it

    <?php
    
    /*
      Template Name: custom404handler.php
    */
    ?>
    
    <?php
    
    $redirects = [
    
        '/addcoach4u.com/timemanagement.html' => '/time-management-articles-books-websites/',
    
        '/communicationsskills.html' => '/communications-and-social-skills-adhd/',
    
        '/complementarytherapies.html' => '/adhd-alternative-treatments/',
    
        '/gettingstartedjeff.html' => '/adhd-the-good-the-bad-how-to-manage-it/',
    
    
    ];
    
    
    if (array_key_exists($_SERVER['REQUEST_URI'], $redirects) {
    
        header('HTTP/1.1 301 Moved Permanently');
    
        header('Location: ' . $redirects[$_SERVER['REQUEST_URI']]);
    
    } else {
    
        header('HTTP/1.0 404 Not Found');
    
        echo '404, Requested page not found';
    
        // or whatever you want your custom 404 to look like
    
    }
    Code (markup):
    and would this

    '/addcoach4u.com/timemanagement.html'
    Code (markup):
    work for both of these file the www and the non www since sadly I have both in some cases?

    http://www.addcoach4u.com/timemanagement.html
    
    http://addcoach4u.com/timemanagement.html 
    Code (markup):
    Do I have the correct code in the htaccess file and the
    custom404handler.php file?
     
    Pete, Aug 18, 2019 IP
  10. mmerlinn

    mmerlinn Notable Member

    Messages:
    2,239
    Likes Received:
    282
    Best Answers:
    6
    Trophy Points:
    260
    #10
    That is why I stated this:

    If you make changes ONE AT A TIME and TEST every change IMMEDIATELY, you will not need to worry about "wrecking" your site. If a change does not work, immediately revert to the last working .htaccess code. At most your site would be down seconds with no lasting issues.
     
    mmerlinn, Aug 18, 2019 IP
  11. Pete

    Pete Well-Known Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    116
    #11
    yes that's my plan test 3 or 4 redirects, see if they work and if not delete them and try figuring out what went wrong. A few minutes of a dead site I won't sweat.
     
    Pete, Aug 19, 2019 at 1:24 AM IP