How to hide .htaccess redirect from user - .htaccess

I have server with domain www.domain.com and multiple sub domains sub1.domain.com, sub2.domain.com. They are all pointing to server root.
I'd like users to access specific folders by subdomains. For example:
sub1.domain.com/someURI => sub1.domain.com/subFolder1/someURI
sub2.domain.com/someURI => sub1.domain.com/subFolder2/someURI
I would like to hide these redirections from users. I tried following .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub1.domain.com$
RewriteRule ^(.*)$ http://sub1.domain.com/subFolder1/$1 [R,L]
RewriteCond %{HTTP_HOST} ^sub2.domain.com$
RewriteRule ^(.*)$ http://sub2.domain.com/subFolder2/$1 [R,L]
It is redirecting correctly only without any URI and redirection is visible.

You have 2 issues that are causing external redirect:
Using R flag in RewriteRule
Using Absolute URL starting with http:// in target
Another issue is that your rewrite rule is unconditional which can cause infinite looping.
You can use these rules:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} =sub1.domain.com
RewriteRule ^((?!subFolder1/).*)$ subFolder1/$1 [NC,L]
RewriteCond %{HTTP_HOST} =sub2.domain.com
RewriteRule ^((?!subFolder2/).*)$ subFolder2/$1 [NC,L]

Related

htaccess redirect subdomain to directory

I need help to write proper rewrite rules in my htaccess files.
I need to redirect something like fr.example.com to example.com/fr, because we recently changed the whole website and the multilingual system is managed differently. The structure and the pages too.
I managed to do that successfully with this piece of code:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
My problem now is to write something more specific for pages, for example :
fr.example.com/discover/foo should go to example.com/fr/bar/foo (different path, nothing consistant)
BUT ! example.com/discover/foo should go to example.com/bar/foo (end of the url is the same in both english and french)
Right now, since I have some common 301 redirects, the french urls aren't redirect properly and lead to the english pages. For example that one :
Redirect 301 /discover/foo /bar/otherfoo
Successfully redirects example.com/discover/foo to example.com/bar/otherfoo but also redirects fr.example.com/discover/otherfoo
How can I write two different rules for english and french? I'll have to write a bunch of different rules since everything is very different from the old subdomain to the new directory, I don't mind.
Thanks !
EDIT
Please note that it's for a wordpress installation, and the htaccess starts with :
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
First the these rules:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
In order to capture bot www & non-www requests for subdomain.
Also this rule :
Redirect 301 /discover/foo /bar/foo
Will capture both requests to domain and sub-domains and using mod_rewrite here is correct not mod_alias so , replace this line with :
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^discover/foo http://example.com/bar/foo [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?(fr)\.example\.com [NC]
RewriteRule ^discover/foo http://example.com/%2/bar/foo [L,R=301]
Note: clear browser cache then test.

Add parameter for specific domains in .htaccess rule

I have two domains that point to the same webserver:
example.cz
example.de
I need:
example.de → example.de/de (but can't be see in address field: example.cz/de)
If it ends up showing in the URL like example.de/de it's OK, but the best solution is just example.de and from server load example.de/de.
I've tried with this:
RewriteBase /
RewriteCond %{HTTP_HOST} example.de$ [NC]
RewriteRule ^$ example.de/de [L,R=301]
but after I click on something on the page I get: example.cz/de and this is problem.
You can use an inner rewrite without R=301 redirect, if you use LAMP server, in /.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} (www\.)?example\.de$ [NC]
RewriteRule ^$ /de/index.php [L]
You'll see in address bar: http://example.de but not http://example.de/de .
Add this to redirect http://example.cz/de to http://example.de .
RewriteCond %{HTTP_HOST} (www\.)?example\.cz$ [NC]
RewriteRule ^de$ http://example.de [R=301,L]

How can I use .htaccess to stop redirecting from subdomains?

I have a domain, "domain.com", and subdomain, "sample.domain.com", and all files related to both are stored in domain.com/folder. How can I use .htaccess to prevent requests from "sample.domain.com" from going to domain.com/folder/index.php?title=sample?
This is what I'm currently using in my .htaccess file:
RewriteEngine on
RewriteCond %{http_host} .
RewriteCond %{http_host} !^www.domain.com [NC]
RewriteCond %{http_host} ^([^.]+)\.domain.com [NC]
RewriteRule ^(.*) http://www.domain.com /folder/index.php?title=%1 [R=301,L,QSA]
But there is one thing - a redirect is external (the browser goes to a new link), and I need to redirect this was on the server side, so that the user thought he was actually working with the subdomain. Can can I accomplish this?
Change this line:
RewriteRule ^(.*) http://www.domain.com /folder/index.php?title=%1 [R=301,L,QSA]
To
RewriteRule ^(.*) /folder/index.php?title=%1 [L,QSA]
The R flag tells the rewrite engine to redirect, and having the http://www.domain.com in the rule's target is also an implicit redirect.

.htaccess simple redirection and too many rediercections: how to solve that?

my site (domain.com) is now located into /var/www/
and you can also access it directly through its IP: 88.88.88.88
I would like to obtain:
when user enter the IP : 88.88.88.88, it redirects to 88.88.88.88/reboot/
if user enters the full domain name, it shows the site as it does now.
I tried with .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} 88.88.88.88
RewriteRule .* http://88.88.88.88/reboot/ [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule .* http://www.mysite.com [R=301,L]
does not work good:
www.mysite.com :GOOD
mysite.com: GOOD
88.88.88.88 ->redirects to 88.88.88.88/reboot : GOOD
but then it generates an error: Error 310 (net::ERR_TOO_MANY_REDIRECTS)
Any clue ?
When you're on 88.88.88.88/reboot, your HTTP_HOST is still equal to 88.88.88.88.
Try adding a RewriteCond rule depending on the REQUEST_URI, which will redirect everything that is not /reboot to /reboot :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} 88.88.88.88
RewriteCond %{REQUEST_URI} !^/reboot/
RewriteRule ^(.*)$ http://88.88.88.88/reboot/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule .* http://www.mysite.com [R=301,L]
The ERR_TOO_MANY_REDIRECTS comes because your rule loops.
Whenever a request comes to host 88.88.88.88 , you redirect it to 88.88.88.88/reboot/ , but /reboot/ uri is also on that host, so it redirects again and loops.
If you only want to redirect / to /reboot/ on host 88.88.88.88 , you need to alter your RewriteRule to this:
RewriteCond %{HTTP_HOST} 88.88.88.88
RewriteRule ^/$ http://88.88.88.88/reboot/ [R=301,L]
If, however, you wish to redirect everything outside of /reboot/ , then you need to add a condition (RewriteCond):
RewriteCond %{HTTP_HOST} 88.88.88.88
RewriteCond %{REQUEST_URI} !^/reboot/
RewriteRule ^/$ http://88.88.88.88/reboot/ [R=301,L]
(keep the rest of the rules intact).

redirect my site from www.foo.bar to cdn.foo.bar

is it possible to have an htaccess rule that will redirect my files from
http://www.mydomain.com/page.html to http://cdn.mydomain.com/page.html but still making the link look like http://www.mydomain.com/page.html
I know masking urls isn't possible, but since they are on the same domain i was wondering if that was possible
Try these rules in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# for http
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ http://cdn.mydomain.com/$1 [L,R]
# for https
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://cdn.mydomain.com/$1 [L,R]
However one caveat that it is an external redirect hence URL in your browser will change to http://cdn.mydomain.com/foo because when you are jumping from one host to another you cannot have internal redirect hence R flag is needed.
No idea about .htaccess but you could use a curl script in PHP.

Resources