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.

www to non-www with .htaccess

Discussion in 'Programming' started by Loque, Aug 20, 2019.

  1. #1
    I currently have this .htaccess: pastebin.com/gfg5ujL0
    On the bottom there are the lines to redirect http urls to https.
    But if I go to my site using https :// www..... it will not redirect to non-www site.
    It may cause a problem for Google I think.

    Do you guys kindly know which is the line to add to make this redirect?
    I searched a lot and each modify I apply makes my site offline! :(
     
    Loque, Aug 20, 2019 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Did you get it sorted? Your pastebin has been taken down.

    To do BOTH http to https and www to non www, I suggest using 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):
    You need to hit it all in one fell swoop making sure that both http and www conditions are intercepted for the SAME RewriteRule with a proper 301. Not sure what you had, but I've been using this one for about a decade and it's never failed me.
     
    deathshadow, Aug 22, 2019 IP
  3. Loque

    Loque Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Thank you for the .htaccess tip :)
    It does not work properly. If I go to my site through https :// www I'm not redirected to the non-www.
    All the other redirects works :)
    Is there a way to solve it through htaccess?
     
    Loque, Aug 23, 2019 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Define "all other". That throws up some warning flags.

    Do you have any other rewriterule's in there that could be interfering? You would/should make sure that the code I provided is the FIRST such rule in your .htaccess.

    Could you put your current .hta up somewhere like pastebin again? Your existing link says you deleted it.
     
    deathshadow, Aug 24, 2019 IP
  5. Loque

    Loque Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    This is the pastebin https://pastebin.com/Z6yCF3ug
    If I put the code on top of the site doesn't work completely :S
     
    Loque, Aug 24, 2019 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Ok, see how you have more than one "RewriteEngine On" in there? Yeah, don't do that.

    Where that first one is:

    
    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule ^forum-([0-9]+)\.html$ forumdisplay.php?fid=$1 [L,QSA]
    
    Code (markup):
    Insert the RewriteCond and RewriteRule I provided between the "on" and your first custom rule.

    <IfModule mod_rewrite.c>
        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]
    
        RewriteRule ^forum-([0-9]+)\.html$ forumdisplay.php?fid=$1 [L,QSA]
    
    Code (markup):
    Naturally getting rid of the copy you put at the end. That SHOULD do the trick.

    That said, with that many rewrite rules you've possibly got some real slowdown issues, and the whole thing is going to be remarkably fragile. That some are inside the ifModule and some outside it is also a bit of a wonk.

    Though the rewriteRule before the engine is even ON? Yeah, that's odd. So much of that claims to have something to do with Google "SEO" when URI rewrites have dick all to do with SEO... Is that some sort of user friendly trickery that someone who knows dick about dick claimed was for SEO? That claim is made a hell of a lot, isn't it?

    Almost looks like some black-hat SEO dirtbag scammed you with disinformation at some point. Apologies if that sounds harsh, but that's what I'm seeing.
     
    deathshadow, Aug 25, 2019 IP
  7. Loque

    Loque Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    That Google SEO code you see is a plugin for a MyBB forum to optimize it on SE :D
    By the way, it still does not work.
    The modified .htaccess is this now :S https://pastebin.com/UxuAjmjA
    If I browse the www. version it still stays on that version.
     
    Loque, Aug 25, 2019 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #8
    Ok, there are two possibilities:

    1) this line:

    RewriteRule ^([^&]*)&(.*)$ https://hackmix.altervista.org/$1?$2 [L,QSA,R=301]

    Is somehow screwing with things. Given it's BEFORE there is even a rewriteEngineOn your server should be sending a 500 error, not working at all.

    2) You have rules in your httpd.conf including a rewriteEngineOn, which are preventing anything in the .htaccess from even running. This might explain how the above rule isn't throwing an error.

    If that is the case, it's possible NOTHING after the above line of code is even being run. EVER.

    3) you don't have mod_rewrite installed in Apache, rendering all those redirect rules non-functional.

    ... and seriously, that redirect nonsense to "optimize for search" is hoodoo-voodoo at best, nonsensical halfwitted gibberish at worst. Somebody was talking out their backside when they told you to do that.
     
    deathshadow, Aug 25, 2019 IP