简体   繁体   中英

.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, eg:

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM