Show contents of other folder based on subdomain htaccess - apache

When i request:
subdomain1.domain.com
i want to show the contents of /httpdocs/subdomain1/
When i request
subdomain2.domain.com
i want to show the contents of /httpdocs/subdomain2/
I want to keep subdomain2.domain.com without redirect
i have a Wildcard on my Plesk, i just donĀ“t know how to set up htaccess
i tried this so far, but is not working
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$
RewriteRule ^ http://subdomain.domain.com/%1%{REQUEST_URI}

Don't use http:// in target otherwise it will do a full redirect not just silent rewrite as you want.
You can use:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.domain\.com$ [NC]
RewriteRule ^ %1%{REQUEST_URI} [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$ will ensure we rewrite only once.

Related

How can I set the RewriteCond for the exact URL only?

I need to make a hidden redirect from sitename.dom to sitename.dom2 keeping the rest of string untouched.
For now I use:
RewriteCond %{HTTP_HOST} ^sitename.dom
RewriteRule ^(.*) http://sitename.dom2/$1 [P]
and it works perfectly. But. Due to multilanguage on my website the frontpage has the following path:
sitename.dom2/lang
thats why when user calls sitename.dom he is being redirected (hidden) to sitename.dom2/ and he's getting 404 page.
So, please advise how do I make a strict redirect for exact request only sitename.dom without any further?
I had tried
RewriteCond %{HTTP_HOST} ^sitename\.dom$
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*) http://sitename.dom2\/lang [P]
with no luck.
BTW, inside the website language subpath doesn't affect at all. sitename.dom/lang/page works as good as sitename.dom/page
I had to add the single redirect before all other. And use dom2 instead of dom1 in this rule.
Here is the solution:
RewriteEngine On
#redirect front page only:
RewriteCond %{HTTP_HOST} ^sitename\.dom2$ [OR]
RewriteCond %{HTTP_HOST} ^www\.sitename\.dom2$
RewriteRule ^/?$ "http\:\/\/sitename\.dom2/\lang" [L]
#redirect all other pages:
RewriteCond %{HTTP_HOST} ^sitename\.dom2
RewriteRule ^(.*) http://sitename\.dom1\/$1 [P]
Simply add /lang in your first RewriteRule directive:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sitename.dom$
RewriteRule ^ http://sitename.dom2/lang%{REQUEST_URI} [P]

Htaccess - redirect to folder

I need help with redirecting from subdomain to folder.
I've tried this
RewriteCond %{HTTP_HOST} ^subdomain.company.com
RewriteRule ^(.*)$ http://company.com/subdomain/$1 [L,NC,QSA]
but I can't get it work.
Here is htaccess I'm working with - I'd rather not change the existing rewrite rules, so I'm putting the new rewrite condition right after RewriteEngine on
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]*)$
RewriteRule (.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
# maximally two redirects
RewriteOptions MaxRedirects=2
# whatever domain
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+).([a-zA-Z0-9-]+).([a-zA-Z]+)$
# Condition to distinguish final URL - prevention of endless loop
RewriteCond %{QUERY_STRING} !^(.*)redirect=1(.*)$ [NC]
# path to file: /domain/path
RewriteRule ^(.*)$ /%2.%3/$1?redirect=1&%{QUERY_STRING}
What could be the problem?

Apache redirect subdomain to folder, keep parameters

I have this code in .htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
# If empty subdomain, replace with "www"
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) http://www.example.com/$1 [QSA,L,R=301]
# If subdomain isn't empty and not "www", redirect to "folder"
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301]
#PAGES REDIRECTION
RewriteRule ^(.*)/register/ /index.php?sub=$1&page=register
RewriteRule ^(.*)/register /index.php?sub=$1&page=register
RewriteRule ^(.*)/lostpass/ /index.php?sub=$1&page=lostpass
RewriteRule ^(.*)/lostpass /index.php?sub=$1&page=lostpass
...
(a rule for wildcard subdmains is already in place and working)
If I browse to http://test.example.com it redirects correctly to http://www.example.com/test but when I try to browse to http://test.example.com/register, it actually redirect to http://www.example.com/test/index.php?sub=http://www.example.com/test&page=register which should redirect to http://www.example.com/test/register
What am I doing wrong here? Thanks in advance!
Try adding the L flag to your second redirect rule, similar to how have it in the first.
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301,L]
It looks like your rewritten URI is passing through to the next rule.
Also, I don't think your first two RewriteCond are in the correct spot.

.htaccess rewrite subdomain to directory

Is it possible to use .htaccess to rewrite a sub domain to a directory?
Example:
http://sub.domain.example/
shows the content of
http://domain.example/subdomains/sub/
Try putting this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.example
RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]
For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.example
RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA]
I'm not a mod_rewrite expert and often struggle with it, but I have done this on one of my sites. It might need other flags, etc., depending on your circumstances. I'm using this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains/subdomain
RewriteRule ^(.*)$ /subdomains/subdomain/$1 [L]
Any other rewrite rules for the rest of the site must go afterwards to prevent them from interfering with your subdomain rewrites.
You can use the following rule in .htaccess to rewrite a subdomain to a subfolder:
RewriteEngine On
# If the host is "sub.domain.example"
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
# Then rewrite any request to /folder
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
Line-by-line explanation:
RewriteEngine on
The line above tells the server to turn on the engine for rewriting URLs.
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
This line is a condition for the RewriteRule where we match against the HTTP host using a regex pattern. The condition says that if the host is sub.domain.example then execute the rule.
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
The rule matches http://sub.domain.example/foo and internally redirects it to http://sub.domain.example/folder/foo.
Replace sub.domain.example with your subdomain and folder with name of the folder you want to point your subdomain to.
I had the same problem, and found a detailed explanation in http://www.webmasterworld.com/apache/3163397.htm
My solution (the subdomains contents should be in a folder called sd_subdomain:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} subdomain\.domain\.example
RewriteCond $1 !^sd_
RewriteRule (.*) /sd_subdomain/$1 [L]
This redirects to the same folder to a subdomain:
.httaccess
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.example$ [NC]
RewriteRule ^(.*)$ http://domain\.example/subdomains/%1
Try to putting this .htaccess file in the subdomain folder:
RewriteEngine On
RewriteRule ^(.*)?$ ./subdomains/sub/$1
It redirects to http://example.org/subdomains/sub/, when you only want it to show http://sub.example.org/.
Redirect subdomain directory:
RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
For any sub domain request, use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.band\.s\.example
RewriteCond %{HTTP_HOST} ^(.*)\.band\.s\.example
RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-z\-]+)
RewriteRule ^(.*)$ /%1/$1 [L]
Just make some folder same as sub domain name you need.
Folder must be exist like this: domain.example/sub for sub.domain.example.
Edit file .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess rewrite and subdomains

I have a subdomain setup as onlinedev.domain.com
I need to use htaccess to rewrite to domain.com/online_content, while still showing onlinedev.domain.com in the address bar (SSL is for onlinedev.domain.com).
this is what I currently have that is very close:
php_flag display_errors off
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} onlinedev\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
RewriteCond %2<->%3 !^(.*)<->\1$ [NC]
RewriteRule ^(.+) /%2/$1 [L]
This correctly rewrites to domain.com/onlinedev, but if I try to change the RewriteRule to:
RewriteRule ^(.+) /online_content/$1 [L]
I get an error
I understand that there are typically better ways to do this subdomain work, but without getting into server config and DNS details, I need to do it with htaccess.
And yes, I do need to rewrite to a directory that has a different name than the subdomain.
Well, I figured it out.
The issue was that I was causing an infinite loop.
Once the rewrite had happened, it was still trying to rewrite to the directory.
Here is my new htaccess that took care of it:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} onlinedev\\.domain\\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/online_content/
RewriteRule ^(.+) /online_content/$1 [L]
Notice that I added a check to make sure that the REQUEST_URI is not the name of the directory I am rewriting to.
Try this rule:
RewriteCond %{HTTP_HOST} =onlinedev.example.com [NC]
RewriteCond $0 !^online_content/
RewriteRule .+ /online_content/$0 [L]