.htaccess RewriteRule for WordPress: map example.com/yyyy/iframe/ to example.com/yyyy?iframe=1

In a WordPress website, all URLs with a trailing /iframe/ should be rewritten to ?iframe=1, e.g.:

mysite.com/page1/iframe/ should be rewritten as mysite.com/page1?iframe=1

Read More

For fun, I tried the following:

RewriteRule ^(.*)/iframe/$ http://microsoft.com [L]

Which is doing what I expected. The RegEx should be correct.
Next, I tried this:

RewriteRule ^(.*)/iframe/$ /$1?iframe=1

In context of WordPress’ own rules, the complete file looks like this:

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

# Here goes my line:
RewriteRule ^(.*)/iframe/$ /$1?iframe=1

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

# END WordPress

But, despite all my tries and variations, the only thing I constantly get, is a 404.
Where am I going wrong?

Related posts

Leave a Reply

1 comment

  1. When you do the redirect to ?iframe=1, you didn’t add the [R] flag, which means the URL will not change in the address bar. This in turn means that WordPress will see “/iframe/” as part of the URL, and tries to find a page or post with that URL. It doesn’t find one, so it gives you a 404.

    So modify your rule like this:

    RewriteRule ^(.*)/iframe/$ /$1?iframe=1 [R,L]
    

    The L flag makes sure that the processing of the rules ends after this rule, then starts over again. This is required to make the R kick in and actually rewrite the URL before it gets to the WordPress part.

    This does mean that ?iframe=1 will remain visible in your URL.