i have 2 websites let says (example1.com and example2.com).
if the visitor write in the url any number for example (example1.com/123456) i want it to re-directed to (example2.com/123456) so the domain only replaced not the numbers the visitor written.
i need that happen using htaccess file or any simple methods without database or save any data because i have only HTML pages, and its hard to me to do it because i am still learning.
CodePudding user response:
All you need is a redirection on protocol level. That is possible with all usual http server's, the exact solution depends on which http server you are using to serve the domain "example1.com".
Since you tagged your question .htaccess
and mod-rewrite
I assume that you are using the apache http server. If so you can implement a rule like that one:
RewriteEngine on
RewriteRule ^/?(\d )$ https://example2.com%{REQUEST_URI} [R=301,L]
It will redirect all requests to example1.com
that use a path that consists only of digits to the second domain while preserving the requested path. An external redirection will get performed using a http status 301 ("moved permanently") as a response to the first request to example1.com
.
If both domains are served by the same http server and you do not have setup separate virtual hosts you probably have to add a condition to that to make sure the rule only gets applied to requests to the first domain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www.)?\.example1\.com$
RewriteRule ^/?(\d )$ https://example2.com%{REQUEST_URI} [R=301,L]
Preferably such a general redirection rule should get implemented in the central http server's host configuration responsible for serving example1.com
. If you do not have access to that you can indeed use a distributed configuration file instead, often called ".htaccess". If so you need to enable that feature beforehand using the AllowOverride
directive. The configuration file needs to be located in the hosts top level DOCUMENT_ROOT
folder and it needs to be readable for the http server process.