htaccess rewrite for author query string when WP is in subfolder

I have successfully forbidden access to any kind of author pages whether trough /author/username/ or the ?author={#id} query string.

I did this with this added to the beginning of my htaccess file:

<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_URI} ^/author/
    RewriteRule .* - [F]
    RewriteCond %{QUERY_STRING} ^author=([0-9]*)
    RewriteRule .* - [F]
</IfModule>

But the same doesn’t work when wordpress is physically in a subdirectory.

The first part works:

RewriteCond %{REQUEST_URI} ^/subdir/author/
RewriteRule .* - [F]

But no matter how I try to edit the second part, the /subdir/?author=1 takes me to the /subdir/usernumber1/ and that is forbidden alright, but this defeats the whole purpose of this.

Any ideas?

Edit:

Yes, I was trying to prevent user names from showing.

In the last moment yesterday I was able to come up with a solution:

<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_URI} ^/subdir/author/
    RewriteRule .* - [F]
    RewriteCond %{REQUEST_URI} ^/subdir/
    RewriteCond %{QUERY_STRING} ^author=([0-9]*)
    RewriteRule .* - [F]
</IfModule>

I may be able to shorten it based on the answers below(for which I’m very thankful).

And yes this is placed in the subdir.

The first snippet was placed in the root dir, which did not worked(or maybe some of the solutions that I tried along the way actually worked but I had the redirect to the author page already cached, I don’t know for sure).

5 Answers
5

The question is about doing this with .htaccess, but why not disabling author pages from within WordPress instead?

This would achieve the same result while making the subdirectory concern irrelevant altogether (and also works regardless of permalink structure settings)

Sample code that sets all urls to author pages to a 404 error:

add_action( 'template_redirect',
        function() {
                if ( isset( $_GET['author'] ) || is_author() ) {
                        global $wp_query;
                        $wp_query->set_404();
                        status_header( 404 );
                        nocache_headers();
                }
        }, 1 );
add_filter( 'author_link', function() { return '#'; }, 99 );
add_filter( 'the_author_posts_link', '__return_empty_string', 99 );

(code taken from this plugin: Disable Author Archives)

Leave a Comment