I'm setting up apache 2.4 (docker) as a reverse-proxy to distribute different subdomains to different docker services. I redirect http-requests to https using the Redirect directive. One specific URL-path (the part after the domain), however, should not be redicted to https, but served with files from a specific directory. I'm trying to accomplish this using the Alias directive, which does not work.
I'm assuming that Redirect overrides Alias. Is that true?
And how could I accomplish my goal if this is the case?
<VirtualHost *:80>
ServerName service.example.com
Alias /exception/ /var/www/exception
Redirect permanent / https://service.example.com/
</VirtualHost>
I expected this to work, but it does not.
From mod_alias docs:
First, all Redirects are processed before Aliases are processed, and
therefore a request that matches a Redirect or RedirectMatch will
never have Aliases applied. Second, the Aliases and Redirects are
processed in the order they appear in the configuration files, with
the first match taking precedence.
To make sure that /exception/ is not matched, use RedirectMatch which allows regex patterns:
RedirectMatch permanent "^/(?!exception/)(.*)" "https://service.example.com/$1"
Related
This is all taking place on a LAMP stack within a <VirtualHost *:80> context. I'm using
RedirectPermanent / https://www.example.com/
to redirect all http requests to https. I also have old incoming broken "http://..." links which I want to redirect, and this seems like the best place to do them, rather than on the https side, which would involve sending a pointless redirect to a not-found https page.
If I stack Redirects like this:
RedirectPermanent /specific/2020/url1.php https://example.com/2021/url1.html
RedirectPermanent /specific/2020/url2.php https://example.com/2021/url2.html
RedirectPermanent /specific/2020/url2.php https://example.com/2021/url3.html
RedirectPermanent / https://example.com/
Is there any potential for conflict between the "url#" redirects and the "/" redirect?
I can't find anything in Apache's documentation to explain how these rules would be interpreted in combination. Does a pattern match for "url1" terminate the script in the way, for example, that the "L" flag does with RewriteRule ?
Or is there the possibility that the "/" -> https line gets applied because it matches also?
I think I've found the answer here:
https://httpd.apache.org/docs/2.4/mod/mod_alias.html
The doc says:
First, all Redirects are processed before Aliases are processed, and therefore a request that matches a Redirect or RedirectMatch will never have Aliases applied. Second, the Aliases and Redirects are processed in the order they appear in the configuration files, with the first match taking precedence.
I'd prefer more explicit wording, but I interpret this as saying when a match occurs, the subsequent Redirects are ignored.
I registered a expired domain to forward all incoming links to another domain. The problem is: many inlinks are placed on subdomains, for example: axa-art.cdn.contento-v41.eu/axa-art/0eee9cec-58cb-45b2-a4e2-b5f73920068e_091216_axa+art_classic+car+study_de_rz.pdf
I am looking for a 301 redirect rule in htaccess that forward any url (no matter on main domain or subdomain) to "new-url.tld"
axa-art.cdn.contento-v41.eu
axa-art.cdn.contento-v41.eu/slug
any-subdomain.contento-v41.eu
any-subdomain.contento-v41.eu/slug
all of this example above should
forward to this exact URL: new-domain.tld
Question 1:
Is it possible to create a "general" rule and place it into htaccess of the main directory?
Question 2:
Or do i have to write a specific rule for each subdomain?
Question 3:
Do I have to create a sub-directory and create a separate htaccess in every sub-directory for each subdomain I want to add redirection-rules?
Help or suggestions are highly appreciated. Thank you very much for your help in advance.
This isn't just a .htaccess question. In order for your server to receive requests to <any-subdomain>.example.com the necessary DNS and server config directives need to be in place. If the request doesn't reach your server then you can't implement a redirect in .htaccess.
So, I suspect that these subdomains are not even resolving?
You either need to create the necessary DNS A records and ServerAlias directives one by one for each hostname (ie. subdomain) or create a "wildcard" DNS A record (and ServerAlias *.example.com directive in the vHost). But then you still have an issue with these hostnames being covered by an SSL cert if you need to redirect from HTTPS.
You can then create the necessary redirect in .htaccess. Although, since you need access to the server config (or a using a control panel that does this for you) to implement the directives above, you should also implement this redirect in the server config also.
For example, at the top of your .htaccess file, before the existing directives (or in your vHost):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^new\.example$
RewriteRule ^ https://new.example%{REQUEST_URI} [R=301,L]
The above states... for any request that is not for new.example then 301 redirect to https://new.example/<same-url>.
However, if you have access to the server config and this other domain is configured in its own vHost container then the redirect can be simplified:
Redirect 301 / https://new.example/
UPDATE#1:
this rule does forward any URL form the main domain to the new domain.
# Permanent URL redirect- by netgrade
RewriteEngine on
RewriteCond %{REQUEST_URI} !https://www.marco-mahling.de/$
RewriteRule $ https://www.marco-mahling.de/ [R=302,L]
The rule I posted above should probably replace your existing rule entirely.
Yes, your rule does redirect every URL to the root of the new domain, but it is arguably incorrect. The RewriteCond directive is superflous and isn't actually doing anything. The REQUEST_URI server variable contains the URL-path, it never contains the scheme + hostname. So, the RewriteCond directive you've posted will always be successful.
If that is the rule you currently have then it would already redirect everything. In which case your problem would seem to the necessary DNS and server config directives as mentioned above.
From your directives, I assume that the other domain actually points to a different server (or different vHost on the same server). Otherwise, this would have resulted in a redirect-loop. In which case, you only need the much simpler Redirect directive that I posted above.
UPDATE#2: That works fine BUT the incoming links are still not forwarded cuz of a "%" in the url: https://axa-art.cdn.contento-v41.eu/axa-art%2F0eee9cec-58cb-45b2-a4e2-b5f73920068e_091216_axa+art_classic+car+study_de_rz.pdf
It's actually because of the %2F - an encoded slash (/) in the URL-path. By default, Apache will reject such URLs with a 404 (for security reasons).
To allow encoded slashes in the URL you would need to set AllowEncodedSlashes On in the server config (or vHost container). You cannot set this in .htaccess. (The server generated 404 occurs before .htaccess is even processed.)
However, I would express caution about enabling this feature. (Is there a specific requirement here? Are you recreating these documents on the new server?)
If this request was intended to map directly to a PDF file on disk then this actually looks like an incorrectly URL encoded request, since a slash / is not a permitted filename character on either Windows or Linux.
If you enable AllowEncodedSlashes then the above RewriteRule will redirect the request to /axa-art/0eee9cec....pdf - note the %-decoded / in the resulting URL. You would need to take additional steps to maintain the URL-encoding (if that was required), but as I say, that looks like a mistake to begin with.
How can I write the Apache configuration to make two different sites on the same domain through a slash
Example:
site.ru - 1 site,
site.ru/app - 2 site
In Apache configuration, you should specify full URL without the part which goes after the slash, i.e.:
ServerName "site.ru:80"
It is not possible to define site.ru/app as a separate virtual host.
However, you can simply move the content of the second website to a subdirectory of the first one. For example, if:
ServerName "site.ru:80"
DocumentRoot "/var/www/httpdocs"
Then /var/www/httpdocs/app will be the directory with the content of the second website.
Another option is to create two virtual hosts, and then to add a rewrite rule to the first domain configuration.
Let's take two domains: site.ru and siteapp.ru. Requests to site.ru/app can be redirected to siteapp.ru using the following on site.ru:
RedirectMatch 301 ^/app/(.*)$ http://siteapp.ru/$1
I'm relatively new to Apache rewrite rules. What I need to do I think should be relatively easy, but I could use a bit of help.
I have a number of name based virtual hosts defined in my Apache configs, for developers to test new feature branches, and I have a wildcard DNS CNAME setup to direct traffic.
Everything works as it should when a request matches the ServerName in one of the virtual hosts. However if the hostname in the request doesn't match any explicitly defined virtual hosts, it automatically uses the first virtual host.
This is quite confusing, as a developer may think they're accessing the correct virtual host, when in fact they're not.
What I'd like to do, is define a rewrite rule in the first virtual host, so that if the hostname in the request doesn't exactly match the defined "ServerName", it will redirect it.
For example...
If I have 2 virtual hosts defined like so...
<VirtualHost *:443>
ServerName default.mydomain.com
...
</VirtualHost>
<VirtualHost *:443>
ServerName my-feature-1.mydomain.com
...
</VirtualHost>
and a developer is trying to request the site for their new feature, but spell it incorrectly, i.e.
https://feature-1.mydomain.com
The first virtualhost will silently serve the request, and they may be none the wiser, and wonder why their new feature code appears not to be working.
What I would like it to do instead. I'd like to redirect them to...
https://default.mydomain.com
so that it's obvious they've misstyped the URL.
I've got it to work with the following rewrite rule in the first virtual host...
RewriteCond %{HTTP_HOST} "!^default\.mydomain\.com" [NC]
RewriteRule ^/(.*) https://default.mydomain.com [L,NE,R=301]
This works as the default for any request not explicitly matched by the subsequent dynamic virtual hosts but also redirects to the preferred default URL. I don't want to actually serve any content on undefined URL's.
The only thing that would improve it slightly would be to not have to duplicate the domain name in the rules.
I have a site at blog.foobar.com that I have closed down, and I want any
page requested there to be forwarded to www.foobar.com
I want my VirtualHost config to do this for me. I currently have the following lines that does nearly what I want but not exactly:
redirect permanent / http://www.foobar.com
Unfortunately what happens is that if I ask for blog.foobar.com instead of forwarding to www.foobar.com it serves the pages on blog.foobar.com instead.
Is there a way doing this in the VirtualHost config or should I use a .htaccess file instead?
Regards
Steve
You can use the Redirect directive in the context of either a VirtualHost or a .htaccess file. However, what you probably want is a RedirectMatch:
RedirectMatch permanent (.*)$ http://www.foobar.com$1
With that inside your blog.foobar.com VirtualHost, any request to blog.foobar.com would be directed to the same page on www.foobar.com, ie. blog.foobar.com/my/page would go to www.foobar.com/my/page.