Home > Mobile >  Redirecting subfolder AND adding a querystring to the resulting url
Redirecting subfolder AND adding a querystring to the resulting url

Time:03-14

I've moved a wordpress installation from a subfolder to the domain root. I've redirected that subfolder successfuly via .htaccess but I'm completely unable to add a query string to it so I know when the client is coming from an old link while keeping any previous query string the request had.

The (only) code I have in the .htaccess file after the wordpress directives is:

<IfModule mod_rewrite.c>
RewriteEngine On    
RewriteCond %{HTTP_HOST} ^nbek.org/blog$ [OR]
RewriteCond %{HTTP_HOST} ^nbek.org/blog/$
RewriteRule (.*)$ https://nbek.org/$1?sublog=nox [R=301,QSA,L]
</IfModule>

I've also tried:

<IfModule mod_rewrite.c>
RewriteEngine On    
RewriteCond %{HTTP_HOST} ^nbek.org/blog$ [OR]
RewriteCond %{HTTP_HOST} ^nbek.org/blog/$
RewriteRule ^(.*)$ $1?sublog=nox [QSA]
RewriteRule (.*)$ https://nbek.org/$1 [R=301,L]
</IfModule>

With no success at all. What am I doing wrong?

CodePudding user response:

The (only) code I have in the .htaccess file after the wordpress directives is:

<IfModule mod_rewrite.c>
RewriteEngine On    
RewriteCond %{HTTP_HOST} ^example.com/blog$ [OR]
RewriteCond %{HTTP_HOST} ^example.com/blog/$
RewriteRule (.*)$ https://example.com/$1?sublog=nox [R=301,QSA,L]
</IfModule>

As stated in comments, these directives cannot be doing anything since the conditions (RewriteCond directives) will never match. The HTTP_HOST server variable contains the value of the Host HTTP request header - this does not contain the URL-path. So, example.com/blog can never match.

You've also put these directives in the wrong place, they need to go before the WordPress code block, not "after". By placing this rule "after the Wordpress directives", unless /blog still exists as a physical directory then (again) these directive will never actually do anything since they will never even be processed (the WordPress code block catches the request and rewrites it to the front-controller at which point processing effectively stops). If /blog still exists as a physical directory then the preceding WordPress code block should ignore the request, allowing this rule to be processed.

I can assure you the redirection is done fine. A bit slowly but fine.

This might suggest the redirect is actually being performed by WordPress itself, not .htaccess. However, this "should" be indicated in the HTTP response headers of the redirect response.

To redirect all requests from a /blog subdirectory to the root and include an additional URL parameter is just a one-liner.

For example, before the WordPress code block:

# Redirect "/blog/<anything>" to "/<anything>?sublog=nox"
RewriteRule ^blog(?:$|/(.*)) /$1?sublog=nox [QSA,R=301,L]

# BEGIN WordPress
:

Or, ensure that it always redirects to HTTPS and the canonical hostname by including the scheme and hostname in the redirect (as you have done):

RewriteRule ^blog(?:$|/(.*)) https://example.com/$1?sublog=nox [QSA,R=301,L]

This will redirect /blog to /?sublog=nox and /blog/foo?bar=1 to /foo?sublog=nox&bar=1 (preserving the initial query string).

The RewriteRule directive itself checks the URL-path in the first argument (ie. ^blog(?:$|/(.*))). The additional "complexity" in the regex is that this will match both /blog (no trailing slash) and /blog/<anything> and still preserve the slash prefix in the substitution string without duplication.

The QSA flag appends the original query string (if any) from the initial request onto the end of the substitution string.

Additional notes:

  • You do not need any additional conditions (RewriteCond directives) before the rule.
  • You do not need to repeat the RewriteEngine On directive, since this should already occur later in the file, inside the WordPress code block.
  • You do not need the <IfModule> wrapper around these directives. These directives are not optional.
  • Related