How to reflect another site in my site using URL Rewriting? - linux

I think my doubt is simple, but I could not find the answer.
I have a linux server that hosts the domain http://www.DomainOne.com
And I have a second server based in Windows plataform that hosts the domain http://www.DomainTwo.com
What I want to do is: When I access the DomainOne.com, it shows all contents of DomainTwo.com, but WITHOUT redirecting the page.
In this case, if I access DomainOne.com/defaultPage.asp?Attribute=test&foo=bar
the URL in browser will be exatly this url, but, the URL showed to the user will be
DomainTwo.com/defaultPage.asp?Attribute=test&foo=bar
Is it possible?
I can't move my pages from DomainTwo to DomainOne because my pages is based in ASP, that works just in Windows. Than, I think that the only solution is to reflect my URL in the original one.
Thanks Guys.

On DomainOne.com enable mod_rewrite, mod_proxy and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?DomainOne\.com$ [NC]
RewriteRule ^ http://DomainOne.com%{REQUEST_URI} [P,L]

Related

.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>

Rewrite any sub-domain and all files to a single directory

When a user goes to any subdomain of my website, for example test.domain.com or even sdjfhdihdfig.domain.com, I want the page of http://www.domain.com/directory to be shown (this directory will be the same regardless of the subdomain the user goes to).
If the user heads over to any page of any subdomain (for example, test.domain.com/apage.html), I want the page of http://www.domain.com/directory/[PAGE] to be shown (in this case the page www.domain.com/directory/apage.html would be shown, but of course this would change depending on what came after the subdomain).
I found something that seems to solve this problem, however, it redirects the user rather than keeping the subdomain as it is in the URL bar. If I went to test.domain.com/test, I want this URL to stay as it is, but the page located at www.domain.com/directory/test should be served instead.
Does anyone have any ideas about how I could do this with htaccess? Thanks in advance.
Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www)[^.]+\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/directory/$1 [R,L]
Once you are satisfied that the redirect works, you can change the R to R=301 to make it permanent.
This assumes that mod_rewrite is both installed and activated for .htaccess files.
If you are not sure, to check if mod_rewrite is even installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for .htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All
I managed to figure out the solution to my problem eventually.
This is my .htaccess document:
Options +FollowSymLinks
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)?(.+)$
RewriteRule ^$ s/$1/ [L]
When someone goes to example.domain.com, the page at http://www.domain.com/s/ is displayed. When they go to a page like example.domain.com/s/page.html, the page at http://www.domain.com/s/page.html is displayed instead.
If the user goes to www.domain.com, this is not treated as a subdomain and instead my main website homepage is displayed.
I've set up a quick script so if someone decides to go to example.domain.com/page.html, instead of showing the page from www.domain.com/page.html, they are redirected to a 404 page on my server. The script (PHP) for how to do this is below:
if(substr($_SERVER['HTTP_HOST'], 0, 4) != 'www.' AND substr($_SERVER['REQUEST_URI'], 0, 3) != '/s/' AND $_SERVER['REQUEST_URI'] != '' AND $_SERVER['REQUEST_URI'] != '/') {
header("Location: http://www.domain.com/404.php");
}
I got the htaccess file from htaccess get domain name from %{HTTP_HOST} into variable, minus the www and just adapted it a little bit.
I hope this helps someone in the future!

How to redirect file URL's to another site

Is it possible to redirect file URL's? For example, I have a link to txt file on my server. For example:
www.mydomain.com/readme.txt
The domain will not host that file any more soon and I would need to redirect this link to some other location:
www.mydomain2.com/readme.txt
The problem is that the first link is part of some desktop applications and I cannot change the desktop applications but need to redirect the link. Is this possible? Maybe with .htaccess?
I'd suggest using .htaccess personally:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^readme.txt http://mydomain2.com/readme.txt [R=301,L]
</IfModule>

Custom .htaccess rules for web app

Ok, I'm trying to undersand mod_rewrite but I end up always confused with all this, so I'm asking if someone can help me with this custom rules.
My web app will use 3 kinds of pages which currently have these URLs:
url.com/index.php?user=name is for the user profiles
url.com/index.php?page=name is for the front-end pages (about, contact, etc...)
url.com/index.php?dash=name is for logged-in area pages
But I want the first 2 to be url.com/name and the 3rd to be url.com/dashboard/name.
I guess that for the user and page I'll have first to check if page exists, then check usernames and if none of this exists, then return 404.
How can I do this?
Thanks in advance :)
These type of custom rules are defined using Regex.
Check the kind of page using RewriteCond and define RewriteRule accordingly.
Complete information is available here
You'll be better off having 3 URL schemes:
url.com/user/name
url.com/page/name
url.com/dashboard/name
If that is acceptable then you need to enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /webapp/
RewriteRule ^user/(.+?)/?$ index.php?user=$1 [L,QSA,NC]
RewriteRule ^page/(.+?)/?$ index.php?PAGE=$1 [L,QSA,NC]
RewriteRule ^dashboard/(.+?)/?$ index.php?dash=$1 [L,QSA,NC]

http to https redirection using .htaccess not working

I want to redirect http://myip/admin to https://myip/admin. I am trying to use .htaccess to do this (want this only for the admin folder).
Contents of .htaccess placed in the relevant folder:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
(apache mod_rewrite is enabled). Both http://myip/admin and https://myip/admin are visible (but the redirection does not work). Server is Ubuntu.
I have tried few variations of the above rules from the internet but no luck :(
Any inputs would be appreciated.
regards,
JP
Solved it and learnt something new. The AllowOverride option in apache's main config file was not set correctly.
Also, if someone faces such issues 2 points:
1) apache reload may sometime not work as expected. Try restart
2) Clear browser cache and / or refresh page.

Resources