1

Similiar to issue found here. Running debian, apache2, trying to redirect example.org.com to example.org.com/aplication/calculator. I have enabled mod_rewrite hence why IP redirect works.

However if I put in different condition into .htaccess file located in my /var/www/html directory and type in the URL address it proceeds to normal https://example.org.com page. The .htaccess file itself contains following:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^123\.456\.789\.0$
RewriteRule ^(.*)$ https://example.org.com/ [R]
RewriteCond %{HTTP_HOST} ^example\.org\.com$
RewriteRule ^(.*)$ https://example.org.com/aplication/calculator [R]

Similarly to issue I have found here previously, tried the apachectl -S command and the output looks so:

VirtualHost configuration:
*:443                  example.org.com (/etc/apache2/sites-enabled/000-default-le-ssl.conf:5)
*:80                   example.org.com (/etc/apache2/sites-enabled/000-default.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
Define: ENABLE_USR_LIB_CGI_BIN
User: name="www-data" id=33
Group: name="www-data" id=33

I have tried creating example.org.com.conf file in /etc/apache2/sites-enabled folder, then through symlink connecting it with file in /etc/apache2/sites-available similiarly to 000-default.conf file (which to my knowledge causes the IP redirect to work) and putting in AllowOverride all, but it still doesn't seem to have any effect. Thank you for reading.

EDIT: Although specified in my virtual host (AllowOverride all), changed to all also in /etc/apache2/apache.conf file for same folder as suggested here although not recommended. Now it redirects me, but puts me on a redirect loop.

3
  • IP address only requests should go to the default site as a catch-all site. Apache looks at the request packet and moves the request to the site that matches the request. Requests for example.com go to example.com.conf whereas IP based requests go to the default site. You can confirm this in the access logs of the two sites.
    – closetnoc
    Commented May 1, 2018 at 15:38
  • You shouldn't touch 000-default.conf. Leave it as is and create new conf files for each site. Steps 3 and 4 of this guide will walk you through the process: digitalocean.com/community/tutorials/… Commented May 1, 2018 at 19:20
  • you'd better create /etc/apache2/sites-available/example.org.com.conf and then run a2ensite example.org.com.conf (to deactivate run a2dissite 000-default.conf)
    – user84582
    Commented May 2, 2018 at 7:36

2 Answers 2

3

This rewrite rule doesn't make any sense:

RewriteCond %{HTTP_HOST} ^example\.org\.com$
RewriteRule ^(.*)$ https://example.org.com/aplication/calculator [R]

That is what is causing your infinite loops. If A request comes in for https://example.org.com/ it will redirect it to https://example.org.com/aplication/calculator. Then the browser will make a request for that URL which matches the same rule and issues a self-redirect.

You shouldn't have a rule that redirects everything on a domain to a single page on that same domain. It is better and correct to return "404 Not Found" for pages that are not supposed to exist. One solution is just to remove that rule.

Another solution would be to change the rule to just redirect the home page:

RewriteCond %{HTTP_HOST} ^example\.org\.com$
RewriteRule ^/?$ https://example.org.com/aplication/calculator [R]

Instead of using rewrite rules, you could move and rename "application/calculator" to "index.html" which gets it automatically served from the root of your site.

I'll also point out that you misspell "application". It should have two "p"s.

1

Further to @Stephen's answer.

Although specified in my virtual host (AllowOverride all), changed to all also in /etc/apache2/apache.conf file for same folder ...

This should be completely unnecessary and perhaps implies you have a conflict of some kind.

Really, you should have AllowOverride None in the appropriate parent <Directory> container in the server config and an overriding AllowOverride All (or something more specific) in each <VirtualHost> container for the specific DocumentRoot for each site.

Now it redirects me, but puts me on a redirect loop.

Yes, as @Stephen states in his answer.

However, this also tells you that the "IP redirect" in your .htaccess file was also not doing anything to begin with. If you were seeing an "IP redirect" (which you say was "working") then something else must have been doing that. (?)

RewriteCond %{HTTP_HOST} ^123\.456\.789\.0$
RewriteRule ^(.*)$ https://example.org.com/ [R]

Assuming you don't host any domains that start with a number then it is more efficient (regex-wise) to simply check for a single digit at the start of the Host header, instead of matching the entire IP address. For example:

RewriteCond %{HTTP_HOST} ^\d
RewriteRule (.*) https://example.org.com/$1 [R,L]

You presumably also need to redirect to the same URL-path (note the $1 backreference). However, since you have access to the server config, then this should be done directly in the server config instead and trapped with a separate/default <VirtualHost> container. No need for mod_rewrite here. (As @closetnoc suggests in comments.)

RewriteCond %{HTTP_HOST} ^example\.org\.com$
RewriteRule ^/?$ https://example.org.com/aplication/calculator [R]

Providing you aren't hosting multiple domains on the same vHost, then the RewriteCond directive that checks the Host would seem to be redundant?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.