testermaster
testermaster

Reputation: 1065

htaccess 301 custom redirect

can anyone help me to set these two redirects?

1)

From: old-domain.com/forum/idF-forum-name
To: new-domain.com/forum/forum-name.idF

Where idF is a variable number, and "original-name" is a variable phrase

2)

From: old-doimain.com/forum/idF-forum-name/idT-thread-name
To: new-domain.com/forum/idF-forum-name/thread-name.idT

Where idT is a variable number, and "Thread-name" is a variable phrase. Somtimes there're urls with another part after idT-thread-name, example:

old-doimain.com/forum/idF-forum-name/idT-thread-name/#idP

Where idP is a random number. I want to redirect also this kind of URL to "new-domain.com/folder/subfolder/thread-name.idT"

Upvotes: 2

Views: 57

Answers (1)

anubhava
anubhava

Reputation: 786289

Case 1: If there is no .htaccess in /forum/ directory.

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(?:www\.)?old-domain\.com$ [NC]
RewriteRule ^forum/([^/]+)/([^-]+)-([^/]+)/?$ http://new-domain.com/forum/$1/$3.$2 [L,NC,R=302,NE]

RewriteCond %{HTTP_HOST} ^(?:www\.)?old-domain\.com$ [NC]
RewriteRule ^forum/([^-]+)-([^/]+)/?$ http://new-domain.com/forum/$2.$1 [L,NC,R=302,NE]

Case 2: If there is a .htaccess in /forum/ directory.

You can use this code in your /forum/.htaccess file:

RewriteEngine On
RewriteBase /forum/

RewriteCond %{HTTP_HOST} ^(?:www\.)?old-domain\.com$ [NC]
RewriteRule ^([^/]+)/([^-]+)-([^/]+)/?$ http://new-domain.com/forum/$1/$3.$2 [L,R=302,NE]

RewriteCond %{HTTP_HOST} ^(?:www\.)?old-domain\.com$ [NC]
RewriteRule ^([^-]+)-([^/]+)/?$ http://new-domain.com/forum/$2.$1 [L,NE,R=302]

Upvotes: 1

Related Questions