简体   繁体   中英

301 redirect with exceptions (in wordpress site / htaccess)

I used to have a Wordpress blog at the root of my domain name and the permalink structure was always simply example.com/name-of-the-blog-post

Now, i've moved that blog to the /blog/ folder AND added a new Wordpress site to the root of my domain. This site, right now, only has 2 pages: /about and /store.

So I have: - 1 wordpress site at / (with 2 pages only) - 1 wordpress site at /blog/ with a full content of blog posts

  1. I need to make sure that all old blog posts that were example.com/name-of-the-blog-post are now /blog/name-of-the-blog-post

  2. EXCEPT the 2 pages example.com/about/ and example.com/store/ that need to remain as is.

How would I write these in my htaccess? Is it 301 redirects or modrewrites? I've looked online but I've not found this exact case anywhere.

EDIT: The closest thing I've tried that works 1/2 the time is this:

RewriteCond %{REQUEST_URI} !^/index\.(php|html)$ [NC]
RewriteCond %{HTTP_HOST} !^example.com [NC]
RewriteRule ^.*$ http://example.com/blog/$0 [R=301,L]

The challenge is this line:

RewriteCond %{HTTP_HOST} !^example.com [NC]

If I write it like this, then the /about and /store pages work fine, but the blog posts don't redirect (404)

If I write it like:

RewriteCond %{HTTP_HOST} !^example.com/ [NC]

Then the blog posts DO redirect but then the /about and /store pages ALSO redirect to /blog/about and /blog/store.

I ended up not doing it through the htaccess. I exported all the URLs of my blog posts and created a .csv of redirects, and uploaded back into a WordPress Redirection plugin. It didn't take long at all, but in theory, I would rather do it through the htaccess, so any extra info would be useful here.

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