I've got a wildcard DNS record, allowing all forms of *.domain.com, additionally I'm pointing several different domains at the same machine and using rewrite rules to direct requests to sub folders depending upon the url the request originates.
For instance domain1.com point to /sites/folder/domain1, domain2.com points to /sites/folder/domain2
My problem is, that i'm using the Apache var HTTP_HOST in this rule, which includes the sub domain so that sub1.domain1.com points to /sites/folder/sub1.domain1.com and with several hundred sub domains for each domain there is no way to create all those folder possibility.
My question: how to remove all sub-domains from the HTTP_HOST variable? This is what the ruleset looks like thus far:
RewriteRule ^file.xml$ sites/%{HTTP_HOST}/file.xml [L]
Which works, until a sub domain is included in the URL...
To limit your rule to just mydomain.com (ie no subdomains) add a RewriteCond directive before the Rewrite as below
RewriteCond %{HTTP_HOST} ^[^\.]+\.com$ [NC]
RewriteRule ^file.xml$ sites/%{HTTP_HOST}/file.xml [L]
Edit:
Allow any domains, but no subdomains
I think what he wants is to use the variable HTTP_HOST in a rewrite, but NOT with a subdomain:
HTTP_HOST = www.example.com
Rewrite rule:
RewriteRule ^file.xml$ sites/%{HTTP_HOST}/file.xml [L]
is, in actuality:
RewriteRule ^file.xml$ sites/example.com/file.xml [L]
but NOT:
RewriteRule ^file.xml$ sites/www.example.com/file.xml [L]
-Brian
Related
I rewrited a subdomain to a section on my site:
https://music.example.com points to https://example.com/section.php?url=music
RewriteCond %{HTTP_HOST} ^music.example.com
RewriteRule ^(.*)$ section.php?url=music/$1 [L,NC,QSA]
But I need to use the same images, css, etc. for this (and other) subdomains.
https://example.com/images/img.jpg
Should be accessible from https://music.example.com/images/img.jpg
I tried to find a solution but I might not be doing the right search.
RewriteCond %{HTTP_HOST} ^music.example.com
RewriteRule ^(.*)$ section.php?url=music/$1 [L,NC,QSA]
This rule internally rewrites (not "redirects") a request of the form music.example.com/<anything> to section.php?url=music/<anything>. ie. Everything, including all images and CSS etc. This is much more than stated in your description, in which you only want the root directory to "point to" https://example.com/section.php?url=music.
If you only want the root directory to be rewritten then restrict the regex ^(.*)$ to only match the root.
For example:
RewriteCond %{HTTP_HOST} ^music\.example\.com
RewriteRule ^$ section.php?url=music [QSA,L]
NB: Don't forget to backslash-escape the literal dots in the regex.
This now naturally excludes everything else from being rewritten, except for requests to the subdomain's root directory, ie. https://music.example.com/ only.
.htaccess redirect all contents from subdomain to domain
As noted above, this is an internal "rewrite", not an external "redirect". And neither does this rewrite from subdomain to domain. The requested hostname does not change and it is assumed that both the subdomain and domain apex point to the same place on the filesystem.
I have requirement on OHS server(apache) to redirect all requests from domain A to domain B but the pages needs to be rendered from domain A only. Below configuration doesn't seems to work.
RewriteCond %{HTTP_HOST} ^http://portal.mycompany.com$
RewriteRule ^(.*)$ http://newportal.mycompany.com:85/$1 [R=301,L]
Am i doing anything wrong?
For domain redirects could you simply point your new domain B to A using either CNAME or A records in the domain registrar?
There are several issues. You can't do a 301 redirect and expect to stay on Domain A. One way you might be able to get this to work is the use of mod_proxy and the P flag. Also the http_host variable only matches the hostname.
You need to make sure mod_proxy is enabled and you can try this.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^portal.mycompany.com$
RewriteRule ^(.*)$ http://newportal.mycompany.com:85/$1 [P]
In this way your domain A is sending a redirect to user browser, so the page is going to be provided from domain B.
If you have to make domain A providing pages from domain B, but user do not have to see domain B, you may check Proxy and/or ProxyPass rule from mod_proxy.
One more thing, if you proxy domain B thru domain A, you may have to rewrite inpage links, so take a look at ProxyHTMLEnable and ProxyHTMLURLMap
Dot (.) is a special character in the RewriteCond pattern, Escape it using a backslash
Try this
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.portal\.mycompany\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ http://newportal.mycompany.com:85/$1 [R=301,QSA,NC,L]
We're in the process of switching our current site from a multiple domain configuration into a single domain, multiple folder format. i.e.
.co.uk/<uri> is becoming .com/en-gb/<uri>
.com/<uri> is becoming .com/en-us/<uri>
I'm hoping that I'll be able to handle this via a couple of well-crafted .htaccess rules, but I'm not sure of code I'm going to need to achieve this. Can you help?
(PS, I've left the actual domain blank, as we only need to test for the TDL, not the entire domain - although whatever the original domain was need to stay the same, with only the TLD changing - i.e. whatever.co.uk would redirect to whatever.com/en-gb/, whatever2.co.uk would redirect to whatever2.com/en-gb/, and whatever.com would redirect to whatever.com/en-us/)
add the following directives to your .htaccess:
RewriteCond %{HTTP_HOST} (.+)\.co\.uk$
RewriteRule (.*) http://%1.com/en-gb/$1 [R=301,L,QSA]
RewriteCond %{HTTP_HOST} \.com$
RewriteCond %{REQUEST_URI} !^/(en-us|en-gb)/
RewriteRule (.*) /en-us/$1 [R=301,L,QSA]
if you have so many domain TLDs, you maybe want to use RewriteMap to avoid duplicating the first rule for every TLD, RewriteMap will map TLD to Uri string (ex: .co.uk to en-gb),
Got a question about using mod_rewrite to redirect specific subdomains to use another domain. I am looking for the shortest possible way to do this without the need to create a separate rewrite rule for each of my domain names. I will be adding many new domain names (roughly 20-30 domains total).
So let's say my main domain name is example.com and I want to use that domain name for everything. So if any of my other domains are used, they will automatically be redirected to the main domain, preserving the subdomain prefix and the URL path.
Example:
test.example.org => test.example.com
test2.example.co.uk => test2.example.com
test3.example.net/hello/world.php => test3.example.com/hello/world.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.([a-zA-Z]+)\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1\.example.com/$1 [R=301,L]
I am unsure if the above will work correctly or if it is proper syntax. But basically, I would like to have it match the subdomain prefix, then any domain name, and any TLD (.org, .info, .biz, .co.uk, .net, etc.). I would assume it would need to make sure that it is not the correct main domain (example.com) first to prevent a infinite redirect loop.
Also, is there a possibility to check if HTTPS is ON or OFF and set the redirect correctly? If not, I can always have HTTPS set to ON.
Sorry for this confusion, although I want to make sure I get this right the first time without needing to program each and every one of the domains into the .htaccess.
Thanks! :)
(.*) is greedy so you don't want to use that, and I assume that you don't want to recreate an infinite loop.
RewriteEngine on
RewriteCond %{HTTPS}s ^..(s?)
RewriteRule ^ - [E=PROTO:http%1]
RewriteCond %{HTTP_HOST} !^\w+\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(\w+)\.\w+\..+ [NC]
RewriteRule ^/?(.*) %{ENV:PROTO}://%1.example.com/$1 [R=301,L]
That rule will not work because it will redirect any host ending with example.com back to example.com and you'll get a redirect loop. You need to change the regex to look like this, to match anything that doesn't end with .com:
RewriteCond %{HTTP_HOST} ^([^\.]+)\.?([a-zA-Z]+)\.(?:(?!com).)*$ [NC]
I need a mod_rewrite rule to redirect url depending on the hostname they are comming from.
The situation:
We have multiple domains pointing to a same webspace and we need to restrict what the specific host can see/download.
domainname.com/images/logo.jpg and /www.domainname.com/images/logo.jpg should transform into domainname.com/domainname_com/images/logo.jpg
So basically I need a rule/function that replaces the dots in the %{HTTP_HOST} with _ and removes/replaces the www subdomain.
Is there any way to do this with mod_rewrite?
Try these rules:
RewriteCond %{ENV:DOMAIN_DIR} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^images/.+ - [E=DOMAIN_DIR:%2]
RewriteCond %{ENV:DOMAIN_DIR} ^([^.]*)\.(.+)
RewriteRule ^images/.+ - [E=DOMAIN_DIR:%1_%2,N]
RewriteCond %{ENV:DOMAIN_DIR} ^[^.]+$
RewriteRule ^images/.+ %{ENV:DOMAIN_DIR}/$0 [L]
The first rule will take the host and store it without www. in the environment variable DOMAIN_DIR. The second rule will replace one dot at a time; the N flag allows to restart the rewriting process without incrementing the internal recursion counter. Finally, the third rule will rewrite the request to the corresponding directory.