简体   繁体   中英

Wordpress .htaccess Rewrite Rule for URLs after Change Root

WordPress: 3.5.1

How do I modify my .htaccess file to Rewrite/redirect old URL paths to the new paths after placing WordPress in its own sub-directory (using https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory -- Using a pre-existing subdirectory install )?

The Problem I had an older Wordpress install in a folder called http://{www.mywebsite.com URL}/wp. I revised the website and followed the instructions for "moving" the site so now my domain uses simply http://{www.mywebsite.com URL}/ for the files. Wordpress runs everything now (unlike my old site where WP was just the bog link.) The moving instructions worked fine in general.

BUT, I want my old links (from search engines) such as http://{www.mywebsite.com URL}/wp/archives/1534 to redirect to http://{www.mywebsite.com URL}/archives/1534 . This does not occur with the default Rewrite Rules used by WordPress. In psuedo code:

Use the general rewrite rules unless coming from a link where I want to "strip" the /wp from the link.

I tried several rewrite variations citing the Apache mod-rewrite documents but none seem to work. The lines I tried in the sample .htaccess file below are prefaced by #Attempt {number}-- (and I realize that these are commented--in the real file they are uncommented.)

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

#Attempt 1-- 
RewriteRule  ^wp/(.*)$  /$1  [L,QSA]

# Default WP ruls
RewriteBase /wp/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]

#Attempt 2 --
RewriteCond %{REQUEST_FILENAME} wp
RewriteRule (^/wp)(.*) $2 [L]

</IfModule>

# END WordPress

Is there a simple way to achieve this?

Try this -- it will do a full 301 redirect (which you want, since this is a permanent change to the URL):

RewriteRule ^wp/(.*)$ /$1 [R=301,L]

I'd also recommend putting it before the # BEGIN WordPress comment, since I think WordPress will rewrite what's inside there if you change certain settings.

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