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!
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]
Thanks so much, it works!! Just wondering, but is this also possible with a redirect while you don't see the url changing?
@Vincentin This is possible with
P
proxy flag. If proxy module is enebled on your server then simply replace the[R,L]
with[P]
in the RewriteRule above. You will get a 500 server error if the module isn't enabled.Thanks! Unfortunately that doesn't work but maybe I can enable the module. Thanks so much! :)