• I want my .htaccess to drop trailing .HTML for all incoming URLs. Example is

    https://acme.com/blog/sample-topic.html
    … would become
    https://acme.com/blog/sample-topic

    I’ve tried several version of .htaccess rewrite commands but nothing works. Here is my latest attempt:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    # remove the .html extension
    RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
    RewriteRule (.*)\.html$ $1 [R=301]
    </IfModule>
    # END WordPress

    Why isn’t this .htaccess code dropping the trailing .HTML of all incoming URLs?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Is that all of your .htaccess?

    I put those exact lines into mine and they worked fine. I suspect WordPress is capturing the URLs before these Rewrite rules are reached or for whatever reason are never executed.

    Thread Starter frontdesk

    (@frontdesk)

    Here is my full .htaccess:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    ## SEO REDIRECTS
    Redirect 301 /page1.html https://www.acme.com/page1
    Redirect 301 /page2..html https://www.acme.com/page2
    Redirect 301 /page3.html https://www.acme.com/page3
    </IfModule>
    # END WordPress
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    # remove the .html extension
    RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
    RewriteRule (.*)\.html$ $1 [R=301]
    </IfModule>
    # END WordPress

    You can see that I have a few hard-coded individual page redirects in there from earlier which I am leaving in because they are critical. My question here is: how do I get this RewriteRule rule (i.e., to universally change any incoming URL that ends in .HTML by simply dropping the trailing .HTML) to run as expected and change incoming URLs as follows:

    https://www.acme.com/blog/anything.html
    should be rewritten as
    https://www.acme.com/blog/anything

    NOTE that I am running for the first time on a Godaddy managed WP host and – although I discussed it with their tech support to confirm that .htaccess should behave the same – I am not 100% sure that the .htaccess is being processed the same as on a non-managed-wp host.

    My bottom line question remains: do you see any reason why my .htaccess would not automatically drop any incoming “.HTML” suffixes?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    remove the #BEGIN #END comments for the blocks that you created. WordPress uses those blocks to decide what parts of .htacess it can rewrite.

    Yeah, what I thought. Need to re-arrange things:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    ## SEO REDIRECTS
    Redirect 301 /page1.html https://www.acme.com/page1
    Redirect 301 /page2..html https://www.acme.com/page2
    Redirect 301 /page3.html https://www.acme.com/page3
    </IfModule>
    
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    # remove the .html extension
    RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
    RewriteRule (.*)\.html$ $1 [R=301]
    </IfModule>
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

    With the first block at the top, WordPress (index.php) was capturing all URLs before any of the other rewrite rules were seen.

    Thread Starter frontdesk

    (@frontdesk)

    Thanks for the feedback.

    As sterndata suggested I removed the #BEGIN #END comments around my blocks.

    And as MarkRH suggested I rearranged the sections according to his example.

    So MarkRH’s example is what I have most recently been using:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    ## SEO REDIRECTS
    Redirect 301 /page1.html https://www.acme.com/page1
    Redirect 301 /page2.html https://www.acme.com/page2
    Redirect 301 /page3.html https://www.acme.com/page3
    </IfModule>
    
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    # remove the .html extension
    RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
    RewriteRule (.*)\.html$ $1 [R=301]
    </IfModule>
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

    Still the incoming .HTML is not getting dropped (except for my explicit 301 redirects which ARE dropping the .HTML, so at least that part of the .htaccess is being processed).

    Unless there is some obvious incorrect syntax then I am at a loss. Is there some directory permission that I need to do? Or perhaps my RewriteBase is wrong?

    And since this is my first Godaddy managed WP site I haven’t completely ruled them out as a suspect yet (but I have nothing to base that on).

    Any other suggestions?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘.htaccess not dropping .HTML as intended’ is closed to new replies.