Mirror a specific path to another url using .htaccess - .htaccess

I would like to use two separate url's, pointing to two separate websites, using one hosting and one backend.
So far there's only one website (I'll name it domain1.com).
And there's already some mod-rewriting used.
redirecting www.domain1.com to domain1.com
RewriteEngine On
RewriteCond %{http_host} ^www.domain1.com$
RewriteRule ^(.*)$ http://domain1.com/$1 [R=301,L]
using nice urls, so domain1.com/news/item points to domain1.com/index.php?/news/item
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index.php)
RewriteRule ^(.*)$ index.php?/$1 [L]
What I would like to achieve are the following things
domain1.com/specific_folder_name/123456789 should point to domain2.com/123456789 just for cosmetics
domain2.com/123456789 should run /index.php?/specific_folder_name/123456789
It should result in this:
when surfing to domain1.com/param1/param2, the server gives you index.php?/param1/param2
whan surfing to domain1.com/specific_folder_name/123456789 you get redirected to domain2.com/123456789 and the server gives you /index.php?/specific_folder_name/123456789
I hope this is clear enough...

I believe you can get this done via ProxyPass Directive
This directive allows remote servers to be mapped into the space of
the local server; the local server does not act as a proxy in the
conventional sense, but appears to be a mirror of the remote server.
The local server is often called a reverse proxy or gateway. The path
is the name of a local virtual path; url is a partial URL for the
remote server and cannot include a query string.
more details

Related

Redirecting a subdomain to a directory by maintainin original URL via .htaccess

I have a subdomain loja.meusite.com.br and I need traffic to be redirected to meusite.com/loja maintaining the original URL loja.meusite.com.br, via htacces
I tried that. It redirects correctly but does not maintain the original URL "loja.meusite.com.br".
I need to access "meusite.com/loja", keeping the URL "loja.meusite.com.br" in the browser
Options +FollowSymLinks
RewriteEngine on
Options +MultiViews
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^loja.meusite.com.br$ [OR]
RewriteCond %{HTTP_HOST} ^www.loja.meusite.com.br$
RewriteRule (.*)$ http://www.meusite.com/loja$1 [R=301,L]
OK, that means you have two options: 1. you can use an internal reverse proxy or 2. you can simply setup the target http host ("domain") such that it shares the physical file system with those other hosts. Or map both using a symbolic link in the file system. As said the first option means a performance penalty, the second depends on the environment you actually have, so how much you are in control of the system the http server is operated on.
1: using the proxy module (again: note that the proxy module and the proxy http module need to be loaded and enabled, which is not the case with typicl cheap hosting providers):
You either directly use the proxy module in the actual http server's host configurtion:
ProxyRequests off
ProxyPass / http://www.meusite.com/loja/
ProxyPassReverse / http://www.meusite.com/loja/
Or you use the proxy module embedded in the rewriting module:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)loja\.meusite\.com\.br$
RewriteRule ^ http://www.meusite.com/loja%{REQUEST_URI} [P]
2: you can setup the DOCUMENT_ROOT of the http hosts "loja.meusite.com.br" and "www.loja.meusite.com.br" (probably a "ServerAlias" of the former) such that it contains a symlink in the file system which points to the DOCUMENT_ROOT of the http host "www.meusite.com". Depending on the actual logic you implemented in that host this might already be enough.
The advantage: you do not require a second, internal http request for each and every incoming request. But you need to sort out that your application logic gets along with those requests...

htaccess - obtain IP Address from file

I have PHP script which collects the current DHCP address of a certain domain which is written to a file.
I need to read the IP address from that file (or collect it directly from within the .htaccess file) into a section of my .htaccess to allow only that certain IP address to be able to login to my account.
I used to use:
RewriteCond %{REMOTE_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/wp-login\.php$
RewriteRule ^(.*)$ blackhole.html [L,R]
I am now hosted on a LiteSpeed server which does not do reverse DNS lookups so I am unable to use any hostname lookups.
An easy solution required please.
RewriteCond %{REMOTE_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/wp-login\.php$
RewriteRule ^(.*)$ blackhole.html [L,R]
Aside: This code would effectively block that specific user, not "allow" them. You would have needed to negate the first condition. ie. !^example\.com$
However, whether the server is configured to perform hostname lookups isn't necessarily a problem of LiteSpeed itself. It's probably just how the server is configured. Many shared hosts disable this feature for performance reasons.
Unfortunately though, your problem is still LiteSpeed with regards to reading a value from an external file in .htaccess.
You can do something like the following using an Apache expression on Apache 2.4+ to read an IP address from an external file and compare it to the REMOTE_ADDR server variable:
RewriteCond expr "file('%{DOCUMENT_ROOT}/ip-address.txt') != %{REMOTE_ADDR}"
RewriteRule ^wp-login\.php$ /blackhole.html [R,L]
Where ip-address.txt is a file in the document root that contains only an IP address (and no newlines).
HOWEVER, on LiteSpeed this simply does not work unfortunately. It just fails silently (the LiteSpeed way).
Aside:
Although, instead of redirecting to blackhole.html it would be preferable to just serve a 404 or 403 Forbidden. For example:
RewriteRule ^wp-login\.php$ - [R=404]

How to redirect root domain to subfolder (with https) and rest of addon domains to subfolders (without https)

How to redirect root domain to subfolder (with HTTPS) and rest of other addon domains to subfolders (without HTTPS).
Currently I have this .htaccess in root which redirects with HTTPS to the-main-subfolder ok. But my other addon domain, say domain2 also gets redirected to the-main-subfolder.
I would like to redirect domain2 to the-domain2-subfolder without HTTPS.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RedirectMatch ^/$ /the-main-subfolder/
I am not sure if this code is correct as it might me using a wildcard. I got this code from searching on net but there are so many suggestions that I am confused now!
In summary: My main hosting account in root should go to https://www.domain1.co.uk/the-main-subfolder when user types in domain1.co.uk in browser and my addon domain http://domain2.co.uk should go to http://www.domain2.co.uk/the-domain2-subfolder.
You can use additional RewriteConds to define specific redirections:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^example.org$
RewriteRule ^ https://%{HTTP_HOST}/the-main-subfolder%{REQUEST_URI} [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^example1\.org$
RedirectRule ^(.*)$ /example1\.org-subfolder/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^example2\.org$
RedirectRule ^(.*)$ /example2\.org-subfolder/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^host1\.example\.org$
RedirectRule ^(.*)$ /host1\.example\.org-subfolder/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^host2\.example\.org$
RedirectRule ^(.*)$ /host2\.example\.org-subfolder/$1 [L,QSA]
I added a few examples to demonstrate the redability of explicit implementation and that you can do that for both, separate domains and hostnames (sometimes incorrectly called "subdomains"). I would always prefer such explicit notation over generic approaches since you can individually modify things, for example for testing or debugging purposes. Except if you are in a mass hosting situation obviously, then a database based approach makes sense.
Note that the redirection for what you call the "root domain" (example.org here) has a second RewriteCond now. Both conditions are AND-combined per default.
For safety you probably also want to add some more rules to redirect requests to something like https://example.org/host1.example.org-subfolder to the specific domain name, since according to your description you are limited to a single file tree in your hosting account. Same for request to http://test1.example.org/test1.example.org-subfolder/... to eliminate the literal folder name.
Oh, and a warning: the above syntax works for .htaccess style files only. If you have access to the real host configuration then you should always prefer to place such rules in there. However you need a slightly changed syntax then. .htaccess style rules are notoriously error prone, hard to debug and they really slow down the http server. They are only offered as a last option for those without access to the host configuration.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain1.co.uk$
RewriteRule ^ https://%{HTTP_HOST}/the-main-subfolder%{REQUEST_URI} [L,R=301,QSA]
Thanks #arkascha - Everything now works as expected with the above code. I suppose we do not need to mention so called add-on domains here at all because cPanel handles the sub-directories for them internally when we add subsequent domains on the hosting package (i.e. addon domains)!
Just to update that my previous solution partially works as it has few niggles/bugs. So went back to the drawing board and suddenly realised I was unnecessarily trying too hard!!
Deleted the old htaccess file first and followed instruction below..
The solution is already provided by cPanel in something called "Redirects" in Panel Icons.
I just had to enter everything in user interface text boxes like choose domainname = "domain1", old folder = "\", new folder = "https://www.domain1.co.uk/the-main-subfolder" - And just click create the redirect. In doing so it creates a .htaccess file itself automatically. I am sharing this below:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.co\.uk$
RewriteRule ^/?$ "https\:\/\/www\.domain1\.co\.uk\/the-main-subfolder\/" [R=301,L]

Using mod_rewrite to redirect .dev domains

In the Apache 2.4 docs on dynamic virtual hosts, it says:
Mass virtual hosts with mod_rewrite
Mass virtual hosting may also be accomplished using mod_rewrite, either using simple RewriteRule directives, or using more complicated techniques such as storing the vhost definitions externally and accessing them via RewriteMap. These techniques are discussed in the rewrite documentation.
I'm attempting to use mod_rewrite instead of mod_vhost_alias because I want it both ways: localhost/project and project.dev should point to the same folder, but either URL should work.
Here's my latest attempt (currently in an .htaccess), which gets me a lovely 500 error.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.dev$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
If I do
...
RewriteRule ^(.*)$ http://localhost/%1/$1 [L,QSA]
I can access the files, but the URL changes (not what I want). I've tried a variety of permutations with and without slashes, RewriteBase, etc.
To be clear, I want project.dev/index.php and localhost/project/index.php to both be valid non-redirected references to /var/www/html/project/index.php. And I'd like to do this in a dynamic way, so I don't need to enter a new set of rules for every folder.
I'm not fixated on doing this with .htaccess - virtualhosts are ok too as long as they're dynamic and I can still access my sites using the localhost/ scheme and the other machines on the network can connect to the sample sites in the usual way (192.168.1.22/project/index.php).
Try this rule:
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(.+)\.dev$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L]

.htaccess forcing HTTPS

I am trying to force HTTPS on a domain. It must be done using a method that works by domain name and not port number (due to host structure/setup).
My closest attempt was:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^.*$ https://www.mydomain.com/$1 [R=301,L]
This works when typing "mydomain.com" into the address bar, automatically redirecting to "https://mydomain.com" but when I type "www.mydomain.com" it does not work. I assume it is a syntax issue as I am very new to htaccess and have spent about 4 hours trying to create a solution from other's code.
Any chance of a pointer?
To make the setup a little more understandable.
/public_html/ - All files in this folder relate to www.mydomain.com
/public_html/subfolder - These folders contain files also relating to mydomain.com
/public_html/subdomain - These folders contain files relating to www.myotherdomain.com
My other domains are subdomains of mydomain.com for to be listed in the cpanel on the host. For example: subdomain.mydomain.com is the same as www.myotherdomain.com.
Hopefully that clears up the structure.
Your redirect happens whenever a request is made to the exact domain mydomain.com (that's what the RewriteCond is testing for). It doesn't apply to any other domains and doesn't detect HTTPS. Use this instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]

Resources