In my htaccess, I wildcard redirect all subdomains to the subfolder of the same name. However I'm needing to add this for each domain I have. I would like to have this compacted to auto do for absolutely any domain.
I do have this for removing www. from requests:
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*) http://%1/$1 [R,L]
The rule I have for wildcard subdomains to subfolders is:
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.tld$
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ http://domain\.tld/%1/$1 [L,QSA]
Taking note of the way the www removal rule works, using the same behaviour using (.*) and %1 in place of the tld.. wouldn't work because that same behaviour is being used for the subdomain/subfolder.
Is there any way I can achieve this? It'll hugely cut down on my htaccess by not having this repeated for every domain.
Edit:
Ok, so you can have multiple capture groups/regex, though I am confused how to get that to work.
Also found from this page this more tidy version that takes away the second condition, as well as the www part as I remove the www anyway so it doesn't matter from what I can see.
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.tld
RewriteRule ^(.*)$ http://domain.tld/%1/$1 [L,NC,QSA]
Edit 2: Looks like I have a winner here. Took the www removal condition and switched it up and got the logic of how you do it.
RewriteCond %{HTTP_HOST} ^([^.]+)\.(.*)
RewriteRule ^(.*) http://%2/%1/$1 [R,L]
Edit 3: This works for every domain besides 1.. a cz domain. It switches the domain name around to be a subfolder of the damn extension like domain.cz to cz/domain along with any subdomain as a secondary subfolder. I'm honestly so so so confused with this.
Ahh I see what's going on. [^.] Is matching anything before the first dot, so without a sub, it does the tld, and if you have a sub of that tld, it does the top level domain and then the sub of it so you end up with /tld/sub as such that I saw with my cz domain.
My main domains I have pointed to vhosts this rule isn't present in, and my alias domains I have pointed to this vhost where the rule is present, but as they're redirected to my main domains before this rule.. they're not caught by it.
Meanwhile my .cz domain I don't currently have going anywhere and have it pointing to the same vhost as the rule, and so it gets caught by it.
I'm sure someone would be able to come up with regex that ensures tlds get ignored, if you want to have a one vhost approach.. but that's just increasing complexity, especially also with there being different number of extensions like .co.uk and .com. And if you only have a one vhost approach such as redirect domains to subfolder sites.. then they won't get caught by that regex, as long as that is above this rule redirecting subs which you want at the bottom of the htaccess.
So it's working as intended, when I actually have my domains setup in the way I intended, which I just hadn't done for my cz one so far lol.
So this does work fine this rule.
RewriteCond %{HTTP_HOST} ^([^.]+)\.(.*)
RewriteRule ^(.*) http://%2/%1/$1 [R,L]
Bit of an update, I'm switching things to Nginx, so for all of you Nginx user's who are looking to do this, here's how you can achieve this:
######## Wildcard * subdomain to subfolder of same name
server {
listen 80;
listen 443 ssl;
server_name ~^(?<sub>[^.]+)\.(?<domain>.*)$;
return 301 https://${domain}/${sub}$request_uri;
}
########
That's so damn simple!
Related
I have a Multilanguage-Website with 2 domains like www.domain.com and www.domain.de
Now I want to define some RewriteRules like:
RewriteRule ^category/(.*)$ http://www.domain.de/$1 [R=301,L]
But I dont know how I can change it to work for both domains at once. Changing that target just without the domain doesnt work.
Example:
RewriteRule ^category-[0-9]{1,4}/(.*) http://www.domain.de/category/$1 [R=301, L]
This would redirect the user to the .de-domain. Doesnt matter which domain he was on preview pages. So visitors from domain.com would be suddenly on domain.de on some specific pages with this rule.
I want this rule to work for both domains and dont know how to write the target path.
I found a solution.
RewriteBase /
and RewriteRule without domain
So the hosting provider I use doesn't support virtual hosts or anything similar so I'm trying to rewrite my primary domain to a sub-directory. So for example I currently have:
example.com rewritten to example.com/sites/example.com/
This works fine using the following code in .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/sites/example.com/.*$
RewriteRule ^(.*)$ /sites/example.com/$1
The reason for this is that have other domains on the same server that I don't want to be accessible by the primary domain and to keep my server organised.
So I also have example2.com as an add-on domain pointing to /sites/example2.com and that works fine.
However, I also have sub-domains that are pointed at different sub-directories such as projects.example.com pointing to /projects/ but these just get rewritten to the /sites/example.com/ directory.
My question is how do I (and is possible too) resolve this? Thanks in advance!
If I truly understand, you should to add host condition, if user is on primary domain - he will be redirected. Check this, I've modified a code a little bit:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#Here we're capturing host name, if it has only one point - this is not subdomain.
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+\.[a-z]{2,5})$
RewriteCond %{REQUEST_URI} !^/sites/%1/
#Redirecting to the current host name in folder sites
RewriteRule ^(.*)$ /sites/%1/$1 [L]
EDIT:
This was not elegant decision to add regex for domain, it will work only for second level domains, if you're using domains like .co.uk - you have to add them manually in rule like RewriteCond %{HTTP_HOST} ^somesite.co.uk$.
I am trying to force HTTPS on a domain. It must be done using a method that works by domain name and not port number (due to host structure/setup).
My closest attempt was:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^.*$ https://www.mydomain.com/$1 [R=301,L]
This works when typing "mydomain.com" into the address bar, automatically redirecting to "https://mydomain.com" but when I type "www.mydomain.com" it does not work. I assume it is a syntax issue as I am very new to htaccess and have spent about 4 hours trying to create a solution from other's code.
Any chance of a pointer?
To make the setup a little more understandable.
/public_html/ - All files in this folder relate to www.mydomain.com
/public_html/subfolder - These folders contain files also relating to mydomain.com
/public_html/subdomain - These folders contain files relating to www.myotherdomain.com
My other domains are subdomains of mydomain.com for to be listed in the cpanel on the host. For example: subdomain.mydomain.com is the same as www.myotherdomain.com.
Hopefully that clears up the structure.
Your redirect happens whenever a request is made to the exact domain mydomain.com (that's what the RewriteCond is testing for). It doesn't apply to any other domains and doesn't detect HTTPS. Use this instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
I am redirecting one domain to another, but I want to preserve the path in the redirect. So for example, I want to visit www.example.com/services/education/page.html, but my redirect will bring them to www.new-example.com/services/education/page.html. What do I write in my .htaccess file to preserve the path "/services/education/page.html"?
Right now I have:
redirect 301 http://www.example.com/ http://www.new-example.com/
But I'm not sure if that works or not (Can't test yet as I am waiting for domain details etc). I just want to be sure when I put the site live. Is that right or am I way off base?
Thanks!
This should do it:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ http://new-example.com/$1 [L,R=301]
try adding the following to your .htaccess in the root of your example.com domain
RewriteEngine On
RewriteBase /
#for all requests to www.example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com$
#redirect them to new-example
RewriteRule (.*) http://www.new-example.com/$1 [R=301,L]
Your original command uses the mod_alias Apache module, and it would work, though you may want to update it to:
Redirect 301 / http://www.new-example.com/
Removing the exact domain of the current (old) domain means all domains that point to that folder will be sent to the new domain, making that one-line script more robust.
The other answers use the mod_rewrite Apache module. If you have that also installed, that's fine to use, though it's 4+ lines of code compared to one. Additionally, mod_alias is part of the "base" package, so should be on all Apache servers, while mod_rewrite is an optional extension, so some might not have it.
I require some guidance with redirects please:
I have two WordPress installations on one hosting - one each in a separate sub-directory.
I was able to get the control panel to set up redirects for each domain, so that - for example:
www.domain1.com redirected to www.domain1.com/d-one/
and
www.domain2.com redirected to www.domain2.com/d-two/
Which works fine - I'm not too bothered that the path contains a subdirectory (I assume I can't hide it).
However, for convenience of the visitors, I would like to set up redirects so that for example:
www.domain1.com/contact redirects to www.domain1.com/d-one/contact
and
www.domain2.com/contact redirects to www.domain2.com/d-two/contact
... the redirect knows which domain is which.
Seeing as no-one was in any hurry to help :( , I did my own bit of digging elsewhere (something I probably should have done earlier if I weren't in such a flap)...
After exhausting all the threads on this site, I found this site:
http://corz.org/serv/tricks/htaccess2.php
which sorted me right up with this solution under "multiple domains in one root":
#two domains served from one root..
RewriteCond %{HTTP_HOST} domain-one.com
RewriteCond %{REQUEST_URI} !^/one
RewriteRule ^(.*)$ one/$1 [L]
RewriteCond %{HTTP_HOST} domain-two.com
RewriteCond %{REQUEST_URI} !^two
RewriteRule ^(.*)$ two/$1 [L]
and it also helped me to hide the unwanted subdirectories d-one and d-two.
Loverly jubbly!