0

I have the following configuration my apache2.conf file:

DocumentRoot /var/www/html
<Directory / >
        # Options FollowSymLinks
        AllowOverride None
        Require all granted
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
</Directory>

I believe this to be pretty much boilerplate for wordpress sites. If I use curl to ask for /index.php it works just fine but if I ask for the root http://localhost/ it returns nothing in the body and the header looks like this:

> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Date: Sun, 30 Jun 2019 18:31:38 GMT
< Server: Apache/2.4.18 (Ubuntu)
< Location: http://ken.net/
< Content-Length: 0
< Content-Type: text/html; charset=UTF-8
< 
* Connection #0 to host localhost left intact

I'm a bit of a rookie with Apache configs but I see nothing out of the ordinary in it. It is pretty much boilerplate ... and believe it or not it used to work (a few months ago but left this project on the shelf too long).


Adding full apache2.conf:

Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100


KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

HostnameLookups Off

ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

Include ports.conf

DocumentRoot /var/www/html
<Directory / >
        # Options FollowSymLinks
        AllowOverride None
        Require all granted
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
</Directory>

AccessFileName .htaccess
<FilesMatch "^\.ht">
        Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf

The directory mods-enabled looks like this:

access_compat.load  authn_file.load  autoindex.load  env.load          mpm_prefork.load  rewrite.load
alias.conf          authz_core.load  deflate.conf    filter.load       negotiation.conf  setenvif.conf
alias.load          authz_host.load  deflate.load    mime.conf         negotiation.load  setenvif.load
auth_basic.load     authz_user.load  dir.conf        mime.load         php7.0.conf       status.conf
authn_core.load     autoindex.conf   dir.load        mpm_prefork.conf  php7.0.load       status.load

Now that I'm aware of the ./sites-enabled/*.conf statement in the main configuration I have removed the <DIRECTORY></DIRECTORY> instructions from the main config and in the default site configuration (aka, /etc/apache2/sites-enabled/000-default.conf) I have changed to:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@site.com
        DocumentRoot /var/www/html

        <Directory / >
                # Options FollowSymLinks
                AllowOverride None
                Require all granted
                RewriteEngine On
                RewriteBase /
                RewriteRule ^index\.php$ - [L]
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteRule . /index.php [L]
        </Directory>>

        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>
  • Where is the rest of the Apache configuration? – Michael Hampton Jun 30 at 18:44
  • sorry i was going to include but thought maybe it would be noisy ... I'll add it above in one sec – ken Jun 30 at 18:48
  • Always include all relevant information. – Michael Hampton Jun 30 at 18:49
  • You edited the Debian default files instead of putting your own virtual host in sites-enabled?! – Michael Hampton Jun 30 at 18:53
  • @MichaelHampton it's been too long for me to remember what I did; but if that's what it looks like then that's probably right. Is there a problem with that? – ken Jun 30 at 18:58
1

Wordpress and similar like to naively assume that only one domain points there. (And also that the protocol is the same on your reverse proxy and backend. You would have the same problem if your apache was using https, and the wordpress was reverse proxied to some other thing (other host, same host other service)).

So unless you can configure wordpress to stop redirecting you (maybe a plugin, or some php patching), you just have to make sure the URL you set in wordpress is the same one that the user uses to get to your site.

If you want to do something different in a test environment, I recommend using the original production URLs (in your case http://ken.net, not http://localhost), but changing their IP to your test environment in your hosts file (eg. 127.0.0.1 ken.net) on the client. Then testing is easy without reconfiguring urls and having different behavior depending on config.

  • thanks @Peter I was wondering if I should do that; having now done it though I still see the same behavior. :( – ken Jun 30 at 20:37
  • I have now found that this box (a VM on my own Antsle hardware) when hit from my development machine DOES convert (/ to /index.php). I don't understand this but I do know that Antsle VM's can expose ports via an auto-configured reverse proxy (NGINX) but then I've also setup an IP tunnel (via ngrok) and it exhibits the same behavior of not redirecting that I see locally. – ken Jun 30 at 20:41
  • The redirect shown here doesn't look like it came from WordPress. – Michael Hampton Jul 1 at 4:21
  • @ken can you show us the new curl output? It's also normal for many things to redirect to an index rather than just transparently show it or append a / when you didn't have one at the end. – Peter Jul 1 at 18:57
  • and show the command too.. – Peter Jul 1 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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