简体   繁体   中英

Wordpress htaccess RewriteRule not working - even with add_rewrite_rule

I'm trying to add some lines in my Wordpress htaccess file in order to redirect some urls but no matter where I put the line I'm not getting any luck. I've tried testing my regex using http://www.regexr.com/ and it works fine but when I use it in the htaccess file itself nothing is happening.

The URL's are child pages of another page (slug is /wedding-fayre-diary/) - I'd like to delete these pages (including the parent page) but redirect the URL's to prevent 404 errors from happening.

The URL's I'm looking to pattern match look like…

http://findyourdreamdress.co.uk/wedding-fayre-diary/gorgeous-with-curves/
http://findyourdreamdress.co.uk/wedding-fayre-diary/impression-bridal/
http://findyourdreamdress.co.uk/wedding-fayre-diary/ivory-co/
http://findyourdreamdress.co.uk/wedding-fayre-diary/ella-rosa/

I want the URL's to redirect to a blog at this URL…

http://findyourdreamdress.co.uk/blog/

Here is my htaccess code…

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
RewriteRule ^wedding-fayre-diary/(.*) /blog/ [QSA,L]

After having yet another look around I managed to find the answer - I think I was missing a couple of important flags. I changed them to [R=301,L,NC] and put the line above the standard Wordpress rules and everything appears to be groovy. Here's my finished code…

RewriteRule ^wedding-fayre-diary/(.*)$ /blog/ [R=301,L,NC]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

It also worth noting that you can't add flags with the Wordpress add_rewrite_rule function - it works ok for redirecting single urls but if you need to use flags like I do you're better off editing the .htaccess file itself.

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