简体   繁体   中英

Trying to redirect blog.domain.com to www.domain.com/blog/

I'm combining a main site at www.domain.com, and an old wordpress blog at blog.domain.com, into one completely new Wordpress install. I have exported and imported all the old blog posts so that they now live under wwww.domain.com/blog//

I am trying to create one rewrite rule that will map all the old blog posts to their new URLS.

I've tried variations on these SO discussions: DNS/.htaccess files to redirect subdomain to specific folder Apache rewrite rule different if capture is empty

but nothing is working.

The following in my .htaccess file will redirect blog.domain.com to www.domain.com/blog, if there is nothing more in the URL:

RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.domain\.com$ [NC]
RewriteRule ^/?$ http://www.domain.com/blog/$1 [R=301,L]

but if there is anything more in the URL, it does not rewrite the URL at all, and goes to the new Wordpress site's 404 page.

I tried adding a capture to the final RewriteRule line, but then no rewrite occurs, and it just goes to the new homepage but the address bar still reads "blog.domain.com":

RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.domain\.com$ [NC]
RewriteRule ^/(.+)$ http://www.domain.com/blog/$1 [R=301,L]

Is there a way to do what I'm trying to do?

You want to remove the leading slash:

RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/blog/$1 [R=301,L]
# no slash---^

And make the + a * . URI's that are sent through rules in htaccess files have the leading slash stripped off, so ^/(.+)$ would never match.

Replace your rule to this:

RewriteCond %{HTTP_HOST} ^(www\.)?blog\.domain\.com$ [NC]
RewriteRule ^ http://www.domain.com/blog%{REQUEST_URI} [R=301,L]

PS: Make sure this is the 1st rule in your .htaccess.

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