Redirect to a page without changing the url - but AFTER another RewriteRule - apache

I am trying to create a multidomain solution with the following setup:
both domains subdomain1.domain.com and subdomain2.domain.com point to the same directory on the server
if one visits the URL https://subdomain1.domain.com/subdomain-category/subdomain-article he should be redirected to https://subdomain2.domain.com/subdomain-article
this cannot be solved with a simple redirect, as https://subdomain2.domain.com/subdomain-article does not exist
because subdomain1.domain.com and subdomain2.domain.com point to the same directory, https://subdomain1.domain.com/subdomain-category/subdomain-article is identical to https://subdomain2.domain.com/subdomain-category/subdomain-article, but it is needed to "mask" the second one in order to show only https://subdomain2.domain.com/subdomain-article - therefore not changing the URL on the second RewriteRule
So far I tried many different rules & combinations, getting closest to a solution with the following:
RewriteCond %{HTTP_HOST} ^subdomain1\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/subdomain-category/(.*)$ [NC]
RewriteRule ^(.*)$ https://subdomain2.domain.com/subdomain-category/%1 [R=302,NC]
...which works so far.
Now, what is missing are the rules that show the content of the page https://subdomain2.domain.com/subdomain-category/subdomain-article while the URL in the address bar of the browser should read https://subdomain2.domain.com/subdomain-article.
Thanks in advance for any valuable hint/info!

Related

Redirecting without changing the displayed URL

tried to search a lot in StackOverflow but nothing solved my problem.
I got a domain in a server (let's call it mydomain.com) and a subdomain called for example mydomain.com/subdomain. I have configured my DNS to have a redirect, so when I write www.subdomain.com it brings me to www.mydomain.com/subdomain.
Is there a way (via DNS, CNAME, .htaccess, anything else?) to perform my redirection but having my browser still displaying www.subdomain.com in the URL bar?
Sorry for bad english
Put the following code in root .htaccess file :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/subdomain/
RewriteRule ^(.*)$ /subdomain/$1 [L,NE]
Update : as per javidazac comment , i should explain that to questioner , that he means sub directory not subdomain , but i will consider that you have sub directory called subdomain

Apache Mod_Rewrite needs to keep the Path but change the Domain

Background
I have a website that has several other alias domain names, in an effort to streamline and simplify I want all the domains to revert to the one domain name I now advertise -
Main name: www.explorerWorld.co.uk
Other names that load the same content but retain their own address:
www.exploreElsewhere.co.uk
www.exploitedexplodedexplorers.co.uk
what I have is that across the internet there are links in peoples blogs and elsewhere that go to specific pages on my site, but these will be under these different domains.
I have changed the domain Alises under cPanel and this works, for the base address only, but a domain for example www.exploreElsewhere.co.uk/trees.php does not redirect.
Question
I would like these pages to still work but to redirect to the main site -- www.explorerWorld.co.uk -- * but preserving their file path*
so: www.exploreElsewhere.co.uk/plants/trees.php gets seemlessly changed to www.explorerWorld.co.uk/plants/trees.php
My htaccess so far:
RewriteCond %{HTTP_HOST} !^exploreWorlds\.co\.uk$1 [NC]
RewriteRule ^(.*)$ http://www.exploreWorlds.co.uk/$1 [R=301,L]
But this seems to work in an endlessly repeating loop, How can I improve this htaccess ?
I have also looked here but this was not very helpful:
.htaccess change domain but keep path
I have found a solution to this problem was to use %{REQUEST_URI} which is the path given to the server.
(with a pointer from Arco444)
RewriteCond %{HTTP_HOST} !^(www\.)?exploreworlds\.co\.uk [NC]
RewriteRule ^(.*) http://www.exploreworlds.co.uk/%{REQUEST_URI} [R=301,L]
This successfully rewrites the domain part of the URL to force alias URL links to be redirected to my base website address + path
You have a mistake in your RewriteCond. Try
RewriteCond %{HTTP_HOST} !^www.exploreWorlds\.co\.uk$1 [NC]
RewriteRule ^(.*)$ http://www.exploreWorlds.co.uk/$1 [R=301,L]
instead, or remove the tilda from your original snippet

htaccess - passing request uri as a parameter

What I'm trying to do is as follows:
Redirect user to another address using htaccess.
Pass the given URL as a parameter to the new url
So when the user visits any page on http://domainA.com/
he should be redirected to: http://domainB.com/?referer=http://domainA.com/
What I keep failing on, is retrieving the full URL the user came from BEFORE the redirection.
One of the things I tried and failed miserably:
RewriteRule ^(.*)$ http://domainB.com/?referer=%{HTTP_HOST}%{REQUEST_URI}
Thanks in advance!
Edit:
I tried to keep my question simple, as more complex questions tend to remain unanswered,
So I will sharpen my question, although there's a good answer to the original one.
So to be more specific about what I'm trying to achieve:
Both domains are hosted on the same host, and points to exact same files.
So domainA.com/file1.html and domainB.com/file1.html will display the same file.
What I want domainA to do, is to deliver all requests to a file called listener.php.
So that all requests to domainA should be like so:
User enters http://domainA.com/file1.html
Server request behind the scenes is actually: /listener.php?actualRequest=http://domainA.com/file1.html
I want this functionality to be on the server side so that the url will remain normal.
I went for 2 domains as I wanted to avoid redirect loops.
You can use this rule:
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainA\.com$ [NC]
RewriteRule ^ http://domainB.com/?referer=http%1://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
This handles both http:// and https:// URLs.
EDIT: As per edited question you can use:
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainA\.com$ [NC]
RewriteRule !^listener\.php$ /listener.php?actualRequest=http%1://%{HTTP_HOST}%{REQUEST_URI} [L,QSA]

Is my rewrite code correct?

I am trying to write rewrite code for my customer's site. I have no way of verifying if it's correct because I don't have access to the server yet. I know that sounds strange but it's what I have to accept and work around.
I plan to put this in the root htaccess file on the server. Bottom line is this URL does not work:
http://www.regions.noaa.gov/gulf-mexico/index.php/highlights/restore-act-passed/
So when the above fires, I want it to permanently redirect to:
http://www.regions.noaa.gov/gulf-mexico/highlights/restore-act-passed/
Here is what I have
RewriteEngine on
RewriteCond %{HTTP_HOST} ^regions\.noaa\.gov$ [OR]
RewriteCond %{HTTP_HOST} ^www\.regions\.noaa\.gov$
RewriteRule ^gulf\-mexico\/index\.php\/highlights\/restore\-act\-passed\/$ "http\:\/\/www\.regions\.noaa\.gov\/gulf\-mexico\/highlights\/restore\-act\-passed\/" [R=301,L]
I'd appreciate any feedback on this. Thanks.
UPDATE - thanks to all who replied. Here's what I don't understand. I found this code on my web hosting company's code generator. It seems to work:
RewriteCond %{HTTP_HOST} ^designerandpublisher.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.designerandpublisher.com$
RewriteRule ^services.html$ "http\://www.regions.noaa.gov/gulf-mexico/highlights/restore-act-passed/" [R=301,L]
I usually do like this and works fine.
IF user enter in the URL with highlights/restore-act-passed/ THEN will display contents from index.php/highlights/restore-act-passed/ in the browser.
# [NC] Means “No Case”, so it doesn’t matter whether the domain name was written in upper case, lower case or a mixture of the two.
RewriteEngine on
RewriteRule ^highlights/restore-act-passed/?$ index.php/highlights/restore-act-passed/ [NC]
IF the user enter in the URL with index.php/highlights/restore-act-passed/ THEN will display contents from _http://%{HTTP_HOST}/gulf-mexico/highlights/restore-act-passed/
RewriteRule ^index.php/highlights/restore-act-passed/?$ _http://%{HTTP_HOST}/gulf-mexico/highlights/restore-act-passed/ [NC]
You don't need to specify the HTTP_HOST, unless you will have multiple domains coming through here (add-ons, subdomains, parked domains, etc.). If you do want to specify it, it can be simplified to one line:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?regions\.noaa\.gov$
RewriteRule ^gulf\-mexico\/index\.php\/highlights\/restore\-act\-passed\/$ "http\:\/\/www\.regions\.noaa\.gov\/gulf\-mexico\/highlights\/restore\-act\-passed\/" [R=301,L]
Actually, a subdomain doesn't even need the www, but it doesn't hurt. Then, in the rewrite rule, you only need to escape specific metacharacters in the pattern, and none in the replacement string:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?regions\.noaa\.gov$
RewriteRule ^gulf-mexico/index\.php/highlights/restore-act-passed(/)?$ http://www.regions.noaa.gov/gulf-mexico/highlights/restore-act-passed/ [R=301,L]
I also made the last (trailing) / optional. Since you're going to the same domain, there is no need to repeat it:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?regions\.noaa\.gov$
RewriteRule ^gulf-mexico/index\.php/highlights/restore-act-passed(/)?$ /gulf-mexico/highlights/restore-act-passed/ [R=301,L]
The 301 code says to alert search engines that this URL or URI has permanently moved (it will show up changed in a browser address bar, too, so human visitors can choose to rebookmark it).
As this appears to be an SEO URI, presumably it will be translated into a dynamic format (/gulf-mexico/index.php?area=highlights&item=restore-act-passed). That means that the above rewrite has to be done before any SEO-to-dynamic translation. An alternative would be to directly translate it to dynamic format right here, but since you're giving a 301, presumably you want the SEO format to show in a browser or search engine result.

htaccess rewrite rules for subdomain to fetch image directory from main domain

I am new to htaccess configuration but I have been reading and researching for awhile.
I have a mobile subdomain which I wish to fetch images located on my main domain. I am unsure as how to get it to work. My current code is:
RewriteCond %{HTTP_HOST} ^m\.\.website\.com [NC]
RewriteRule ^/avatar/(.*)$ http://www.website.com/avatar/$1 [P]
The image directory looks like this:
/avatar/thumbnail/image.jpg
/avatar/preview/image.jpg
/avatar/full/image.jpg
However, when I try to access m.website.com the images do not load.
Can anyone tell me what I am missing.
Any feedback would be greatly appreciated.
Remove leading slash from your rule and also take out extra DOT from host name condition:
RewriteCond %{HTTP_HOST} ^m\.website\.com [NC]
RewriteRule ^(avatar/.*)$ http://www.website.com/$1 [P,L]
This assumes you have mod_proxy enabled and working.