Warm tip: This article is reproduced from serverfault.com, please click

htaccess redirection of a specific page, not whole domain

发布于 2020-11-30 11:40:19

I need to redirect a page (not whole domain!) to a specific other page that's on another domain, with htaccess. I tried a couple of things but can't make it to work. Can anybody help me out?

I tried this one:

#RewriteEngine On
#RewriteCond %{HTTP_HOST} ^domain.com/example [NC]
#RewriteRule ^(.*) https://otherdomain.com/page [P]

and this one:

#RewriteEngine on
#RewriteCond %{HTTP_HOST} ^domain.com/example/$ [NC,OR]
#RewriteCond %{HTTP_HOST} ^www.domain.com/example/$ [NC]
#RewriteRule ^(.*)$ http://www.mainpage.com/pagename/$1 [L,R=301,NC]

The page that should be redirected is /example

domain.com/example >>> should redirect to: https://otherdomain.com/page

The page that should be directed is accessible with and without www. in front of it, not sure if this is relevant.

Many thanks!

Questioner
Vincentin
Viewed
0
Amit Verma 2020-11-30 21:52:14

Your rule doesn't work because it has an error in the RewriteCond

#RewriteCond %{HTTP_HOST} ^domain.com/example/$ [NC,OR]

This condition will never be met as %{HTTP_HOST} variabe only contains the host header ie. example.com and not the URI string /example/ . You can test URI string using %{REQUEST_URI} variable in a separate RewriteCond .

This should work for you

RewriteEngine on

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI}  /example [NC]
RewriteRule ^ https://otherdomain.com/page [R,L]