1

About 9 months ago i changed my permalink structure in WordPress from the domain.com/year/month/article-name format to the domain.com/article-name one.

I hired someone to do the rewrite rules for me. The person seemed to have done a good job, as all links i tested redirected as expected.

1 month ago I moved my website to a new server. The friend who moved it, first copied my website to his own server, then installed it on a droplet on digitalocean. In this process the individual also made changes to the .htaccess file.

I seem to now have lost all my redirects from the previous change.

I have tried the following:

  1. Based on this article I attempted to make a change to the line RewriteBase / in the WordPress section to RewriteBase /web/. This broke my website, so i changed it back.
  2. Moving the WordPress section to the beginning of the file. It seems to do nothing.

How can I reimplement the former redirect... I unfortunately lost all earlier versions of the file.

# RewriteRule ^9671126861/(.*)$ guzzle-downslope.php [QSA,L]
<ifmodule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 7200 seconds"
  ExpiresByType image/x-icon "access plus 8640000 seconds"
  ExpiresByType image/gif "access plus 8640000 seconds"
  ExpiresByType image/jpeg "access plus 86400000 seconds"
  ExpiresByType image/jpg "access plus 86400000 seconds"
  ExpiresByType image/png "access plus 86400000 seconds"
  ExpiresByType text/css "access plus 8640000 seconds"
  ExpiresByType text/javascript "access plus 864000000 seconds"
  ExpiresByType application/x-javascript "access plus 864000000 seconds"
  ExpiresByType application/javascript "access plus 864000000 seconds"
  ExpiresByType font/ttf "access plus 8640000 seconds"
  ExpiresByType font/otf "access plus 8640000 seconds"
  ExpiresByType application/vnd.ms-fontobject "access plus 8640000 seconds"
  ExpiresByType application/x-woff "access plus 8640000 seconds"
  ExpiresByType application/x-font-ttf "access plus 864000000 seconds"  
  ExpiresByType application/x-font-woff "access plus 8640000 seconds"    
  ExpiresByType application/vnd.ms-fontobject "access plus 864000000 seconds"  
  ExpiresByType image/svg+xml "access plus 8640000 seconds"
  ExpiresByType image/vnd.microsoft.icon "access plus 8640000 seconds"  
</ifmodule>

# BEGIN WPSuperCache
# END WPSuperCache

# BEGIN optimizeMember GZIP exclusions
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{QUERY_STRING} (^|\?|&)optimizemember_file_download\=.+
    RewriteRule .* - [E=no-gzip:1]
</IfModule>
# END optimizeMember GZIP exclusions

# BEGIN rlrssslReallySimpleSSL rsssl_version[3.2.3]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL
# 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

php_value upload_max_filesize 25MB
php_value post_max_size 27MB
php_value memory_limit 30MB

2 Answers 2

1
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Additional rewrite rules go here
RewriteRule ^[0-9]{4}/[0-9]{2}/(.*)$ /$1 [R=301,L]
:

Any custom mod_rewrite directives, such as your "redirect" should go before the # BEGIN WordPress section. You should avoid editing between the WordPress comment markers as WP itself tries to maintain this section and might overwrite your custom edits during a future update, unless you take additional steps to avoid this (such as restricting file permissions etc.).

Aside: The blog article you linked to suggested putting the "additional rewrite rule" in the middle of the rule block. If this was not a WordPress site then that would perhaps be the correct (most logical) place to put it. However, this is WordPress, so there are other caveats (as mentioned above).

You can literally move just that one line to before the # BEGIN WordPress line. The <IfModule> wrapper and RewriteEngine directives do not need to be repeated. For example

# Additional rewrite rules go here
RewriteRule ^[0-9]{4}/[0-9]{2}/(.*)$ /$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase / 
:

You could also consider redirecting to your canonical URL (hostname + HTTPS) at the same time and including this before your HTTP to HTTPS redirect. This would avoid a potential double redirect when requesting HTTP and your old permalink structure. For example:

RewriteRule ^\d{4}/\d{2}/(.*) https://example.com/$1 [R=301,L]

(Although you don't appear to be canonicalising the hostname currently?)

A note about RewriteEngine... whilst it is common to see multiple instances of this directive in the .htaccess file (usually when the application itself is maintaining the code, or when code is copy/pasted), only the very last instance of this directive in the .htaccess file has any effect. So, from a coding perspective, having multiple RewriteEngine directives is entirely superfluous and confusing. eg. Setting RewriteEngine Off earlier in the file does nothing. The same applies to RewriteBase - only the last instance in the file does anything.

As for checking <IfModule mod_rewrite.c>, see this other question:

1

OK. So after having spelled out the question and posted it, I finally found the solution in this blog post.

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Additional rewrite rules go here
    **RewriteRule ^[0-9]{4}/[0-9]{2}/(.*)$ /$1 [R=301,L]**

    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

# END WordPress
4
  • 1
    You should include your custom "redirect" before the # BEGIN WordPress section. Avoid editing between the WordPress comment markers as WP itself tries to maintain this section and might overwrite your custom edits during a future update.
    – MrWhite
    Commented Oct 17, 2019 at 13:45
  • SO I can simply include it above that section as a standalone line?
    – rohrl77
    Commented Oct 17, 2019 at 14:03
  • Yes, that's right - just that one line. You do not need the <IfModule> wrapper, nor do you need to repeat the RewriteEngine directive.
    – MrWhite
    Commented Oct 17, 2019 at 14:06
  • 1
    Thank you very much! Really helpufl advice... because if WP overwrites it i'll start scratching my head again in a few months about why my ranking dropped. Just adjusted it. Would you like to post you comment as an answer? I'll accept it!
    – rohrl77
    Commented Oct 17, 2019 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.