Search code examples
regex.htaccesshttp-redirectmod-rewrite

Simplifying redirect in htaccess


I have redirects:

RewriteRule ^(.*)/thema(.*)$ https://www.newurl.com [R=301,L]
RewriteRule ^(.*)/stichpunkt(.*)$ https://newurl.com [R=301,L]
RewriteRule ^(.*)/author(.*)$ https://www.newurl.com [R=301,L]
RewriteRule ^(.*)/2023(.*)$ https://www.newurl.com [R=301,L]

is there a way to simplify these into one line?


Solution

  • I need to disable category, tag, author and date archives in Wordpress

    This should really be done in WordPress itself. Otherwise WP is still going to generate and publish these URLs (eg. Sitemap, RSS feed, etc.).

    Otherwise, if .htaccess is your only option then you should serve a 404, rather than redirect to the homepage. Whilst a redirect to the homepage is likely to be treated as a soft-404 by Google (and possibly other search engines) it runs the risk of being indexed under these "archive" URLs (and accessible with a site: search).

    For example, at the top of the root .htaccess file (before any existing WP directives):

    # Whatever your custom 404 page is (could be WordPress)
    ErrorDocument 404 /404.php
    
    # Force a 404 for "category, tag, author and date archives
    RewriteRule (^|/)(thema|author|stichpunkt|2\d{3})(/|$) - [R=404]
    

    2\d{4} matches any 4 digit year (in the 2000's).

    The regex matches any of those "words" only when they occur as a whole path segement (not partial matches).

    R=404 - This is not a "redirect" (despite the use of the R flag). The 404 error document is served via an internal subrequest and a 404 HTTP response code is set on the initial response. If these URLs have previously been indexed then consider changing this to a "410 Gone" instead, ie. R=410 or simply G (shorthand flag).