redirect website using htaccess - .htaccess

I have a wesite:
http://site1.com
and a second website:
http://89.89.89.89:8888
I want that when I acces http://site1.com/site2/ to show the content of http://89.89.89.89:8888.
Also I want the URL to remain the same. Ex:
Instead of http://89.89.89.89:8888/Page1.htm i want the URL to be http://site1.com/site2/Page1.htm .
Also when i navigate from Page1.htm to Page2.htm, the URL would be http://site1.com/site2/Page2.htm.
Thank you.

You need to proxypass to do something like that. You need to make sure that you have mod_proxy loaded in apache. Then use th P flag in the rule. You can try it like this.
RewriteCond %{REQUEST_URI} ^/site2 [NC]
RewriteRule ^(.*)/?$ http://89.89.89.89:8888/$1 [L,P]
To enable mod_proxy, you typically just need to uncomment these lines in apache config and reload/restart apache.
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

Related

.htaccess RewriteRule : not all rules work on Ionos [duplicate]

Start by explaining what I'm trying to do:
I've got different pages on my website. Some pages have the same templates so I create one page with parameters to adapt my page: Parameters are called pageview and lang the URL looks like this:
http://mywebsite/home/en <- http://mywebsite/index.php?pageview=home&lang=en
http://mywebsite/page2/fr <- http://mywebsite/index.php?pageview=page2&lang=fr
for example. To dot that, I use the famous .htaccess file and it module rewrite_module for Apache.
I've got also a contact page with a different template. It URL looks like this and here there is only one parameter:
http://mywebsite/contact/fr <- http://mywebsite/contact.php?lang=fr
http://mywebsite/contact/en <- http://mywebsite/contact.php?lang=en
Here is my .htaccess code:
RewriteEngine On
RewriteRule ^contact/([a-zA-Z0-9]+)/?$ contact.php?lang=$1
RewriteRule ^([a-zA-Z0-9]+)$ index.php?pageview=$1 [QSA]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?pageview=$1&lang=$2 [QSA]
The problem is that .htaccess file work for the index.php but not for contact.php
I can fully access to contact.php but the parameter is not detected
Thanks for your help 😀😀 !
EDIT
If I remove index parts to stay only the contact rewriteRule's the problem stay there.
contact.php and index.php are in the root folder
RewriteRule ^contact/([a-zA-Z0-9]+)/?$ contact.php?lang=$1
It looks like you may have a conflict with MultiViews. If MultiViews is enabled then mod_negotiation will rewrite a request for /contact/fr to /contact.php (without any parameters) before mod_rewrite is able to process the request.
Try disabling MultiViews at the top of your .htaccess file:
Options -MultiViews

.htaccess specific url redirect to another domain url [duplicate]

After looking on the internet for about an hour, I didn't find the answer to my question. So I'm searching with the wrong keywords or what I want is not possible.
What I want:
I have multiple domains with different extensions, for example:
mydomain.be
mydomain.nl
Now what I want is that the mydomain.be is redirected to mydomain.nl. The solution for this I have found on the internet and shown below, with the need of .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.be$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain.be$
RewriteRule (.*)$ http://www.mydomain.nl/$1 [R=301,L]
With this code, when you type mydomain.be you will be redirect to mydomain.nl. But also the URL in the addressbar is changed to mydomain.nl. What I want is to keep the URL in the addressbar mydomain.be.
So, mydomain.be:
keep URL
show content of mydomain.nl
How To?
It is possible to get it done via mod_rewrite but make sure mod_proxy is enabled in your Apache's httpd.conf. Once that is done enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.be$ [NC]
RewriteRule ^ http://www.mydomain.nl%{REQUEST_URI} [L,NE,P]
Take note of flag P which is used for handling the proxy request.
Read more about flag: P in mod_rewrite
Another option without hassling with .htaccess would be to point both domains to the same document root or setting one domain as an alias for the other, depending on how you are able to configure your Apache. However, this has downsides:
If your content management system uses absolute URLs a user who clicks on mydomain.nl on a link will be directed to the mydomain.be domain (WordPress does this, as an example).
Search engines punish this behaviour by placing you further down on the search results. at least Google does, they have an interesting blog post about duplicate content. Not sure about competitors.
An example apache config could be:
<VirtualHost *:80>
ServerName mydomain.nl
ServerAlias mydomain.be
DocumentRoot /var/www/mydomain.nl/htdocs
</VirtualHost>

How to use .htaccess URL masquerading technology

Basically, I was wondering if I could take a url like:
http://tai.tskynet.com/?id=book
and rewrite it to
http://tai.tskynet.com/book/
http://tai.tskynet.com/book
like a "fake" folder.
What should I write?
First, you have to make sure that your rewrite_module and dir_module is enabled in apache. Then check your httpd.conf file to make sure that these lines exist:
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule dir_module modules/mod_dir.so
Then set the value for DirectoryIndex to include the target file (index.php as example)
<IfModule dir_module>
DirectoryIndex index.php ...
</IfModule>
Here is the .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|assets|uploads|cloudfront\.php)
RewriteRule ^(.*)$ index.php/$1 [L]
This would forward your URI values to the target file [index.php] then process it there.
In the example, robots\.txt|assets|uploads|cloudfront\.php will not be forwarded to index.php and instead will be treated as normal directory requests.

htaccess: how to get cpanel username out of url and redirect

I have the following htaccess lines...
RewriteRule ^([a-zA-Z0-9]+)?$ index.php?p1=$1 [L]
RewriteRule ^~([a-zA-Z0-9]+)/([a-zA-Z0-9]+)?$ ~$1/index.php?p1=$2 [L]
The first line works fine, its the second line that doesn't work at all...
The first line does this....
domain.com/about -> domain.com/index.php?p1=about
What I'm trying to do with the second line...
if the url is server1.domain.com/~username/about....
I need it to translate to server1.domain.com/~username/index.php?p1=about
Basically, detecting if there is a ~
I am trying to work out my code to allow for the development url of the hostname/~username
Right now it is showing the green apache 404 not found page when trying to visit the website using that code.
Please let me know if you need any more information
Switch the order of the rules and add a RewriteBase:
RewriteBase /
RewriteRule ^~([a-zA-Z0-9]+)/([a-zA-Z0-9]+)?$ ~$1/index.php?p1=$2 [L]
RewriteRule ^([a-zA-Z0-9]+)?$ index.php?p1=$1 [L]
You need to be sure that this .htaccess file is triggered both on requests to pages from domain.com and pages from server1.domain.com.
Also, if you have a .htaccess file in any subdirectories (you shouldn't, based on your problem description), you will have to modify those accordingly, but we would need more information.
You need to place this rule in your httpd.conf:
RewriteEngine On
RewriteRule ^/?(~[a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ /$1/index.php?p1=$2 [L,QSA]
And make sure this line is uncommented in httpd.conf:
LoadModule userdir_module modules/mod_userdir.so
Then make sure index.php is present directly under ~username/

Is it possible to rewrite url to another domain where the files are not located, using .htaccess?

A website is located at http://www.siteone.com
and I'd like to rewrite the url to something like http://www.anothersite.com/siteone or to a subdomain http://siteone.anothersite.com.
The website will be hosted only at http://www.siteone.com and no files will be moved or copied to anothersite.com.
Is it possible to do this using .htaccess or is there another way to do this?
EDIT (Below is what I've done in htaccess in my localhost and it does not seem to work). Not sure what I am doing wrong. I don't want to mess with the live site at this stage.
RewriteCond %{HTTP_HOST} /localhost/mysite [NC]
RewriteRule ^(.*)$ http://www.anothersite.com/siteone/$1 [L,P]
ProxyPassReverse /localhost/mysite http://www.anothersite.com/siteone/
As long as you have mod_proxy loaded, you can reverse proxy requests using the P flag in a rewrite rule. You can do this inside an htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?siteone.com$ [NC]
RewriteRule ^(.*)$ http://www.anothersite.com/siteone/$1 [L,P]
This will make it so when you visit http://siteone.com/some/path/, the request will get reverse proxied to http://www.anothersite.com/siteone/some/path/ and the contents from that request will be sent to the original request. This the URL in one's browser remains http://siteone.com/some/path/.
In order to handle things like redirects, you need to set a ProxyPassReverse so that the location headers get property rewritten:
ProxyPassReverse / http://www.anothersite.com/siteone/
See also, ProxyPassReverseCookieDomain and ProxyPassReverseCookiePath if the site at www.anothersite.com wants to set cookies.
Note: although the ProxyPass directive can only be used in vhost and server config, the ProxyPassReverse directives can actually be used in an htaccess file in conjunction with mod_rewrite.

Resources