简体   繁体   中英

htaccess Redirect - Old Domain to New Domain with Slight Change in link structure

I'm stuck. I don't know much about htaccess and I'm just winging it. Can someone look at the code and tell me what's wrong with it. I simply want to redirect an old site to a new site and the only changes are the domain, a variable that will match between old/new pages and an added word to the permalink structure.

Here's some of the variations I've tried so far:

Options +FolowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*domain.com/matchingword1-(.*)-matchingword2-matchingword3/ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/matchingword1-$1-matchingword2-differentword-matchingword3/ [R=301,L]

Options +FolowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*domain.com/matchingword1-(.*)-matchingword2-matchingword3/ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/matchingword1-%1-matchingword2-differentword-matchingword3/ [R=301,L]

RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^domain\.com/matchingword1-(.*)-matchingword2-matchingword3/$ http://www.newdomain.com/matchingword1-\$1-matchingword2-differentword-matchingword3/\ [R=301? [R=301,NE,NC]

The section (. *) will be the exact same as $1 on the new domain but the permalink is a little different. The part of the permalink that is (. *) will be anything from multiple words and numbers.

For example: matchingword1-this-page-is-1st-matchingword2-matchingword3/ redirects to newdomain dot com/matchingword1-this-page-is-1st-matchingword2-differentword-matchingword3/

The rewrite condition for HTTP_HOST is what is passed in the request's "Host:" header, which only contains the host (and in some cases a port). The Request URI isn't part of HTTP_HOST . Try something like this:

RewriteCond %{HTTP_HOST} ^.*domain.com [NC]
RewriteRule ^matchingword1-(.*)-matchingword2-matchingword3/ http://www.newdomain.com/matchingword1-$1-matchingword2-differentword-matchingword3/ [R=301,L]

The last set doesn't look like it'll work at all.

Edit: comments won't let me insert code

If you want to redirect EVERYTHING, you should use mod_alias' RedirectMatch:

RedirectMatch ^(.*)$ http://www.newdomain.com/$1

If you are serving the directory on the same instance of apache (as in, domain.com and newdomain.com is actually the same server serving from the same directory), then you need to rewrite. Something like this:

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

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