Redirecting without changing the displayed URL - apache

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

Related

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

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!

Redirecting shows wrong url

I'm working a long time on this issue but I still didn't found a solution.
I have on a hosted sever a number of domains. My primary domain is directed to my public_html directory. For my second domain if have created a folder site2.
So www.site1.com is directed to public_html and www.site2.com is redirected to dir site2. So far no problem. Both sites seems to work ok.
But if a navigate to www.site2.com/extra_content the url that is shown is www.site1.com/site2/extra_content. If I use a traling slash on my url, the correct URL (www.site2.com/extra_content) is shown. So I added the solution for the trailing slash. But it doesn't work.
My .htaccess looks like this:
#ADD TRAILING SLASH TO DIRECTORY IF NONE EXISTS
RewriteRule ^([^\.]+[^/])$ http://%{HTTP_HOST}/$1/ [L]
RewriteCond %{HTTP_HOST} ^(www)?site2.com$ [NC]
RewriteCond %{REQUEST_URI} !site2/ [NC]
RewriteRule ^(.*)$ /site2/$1 [NC]
Probably I missed something, so any help would be very much appreciated

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

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.