WebDev WebDev - 1 month ago 8
PHP Question

Htaccess: redirect some pages from one domen to another

I need to redirect some pages from one domen to another. Example:

example.com/help/questions(/show/some_text|none|get params)
to
example2.com/questions(/show/some_text|none|get params)


htaccess:

Redirect permanent /help/questions http://example2.com/questions


But redirect from
example.com/help/questions/show/some_text
doesn't work - I have
example2.com/help/questions/show/some_text
instead
example2.com/questions/show/some_text

With such code I have some problem.

RewriteCond %{HTTP_HOST} example.com/help/questions
RewriteRule (.*) http://example2.com/questions/$1 [R=301,L]


How I can fix it?
All another pages redirect normally.

Answer

%{HTTP_HOST} only matches host name not URI.

You can use:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^help/(questions/.*) http://example2.com/$1 [R=301,L,NC,NE]
Comments