insaner
insaner

Reputation: 1705

apache add trailing slash redirects to different ServerName

In apache 2.2.22, I have multiple local servers setup such that localhostN refers to a server which I can access via local.myserver.com (for different "myserver"s). The problem is that one such server is doing a strange redirect when adding the automatic slash (I'm guessing via mod_dir), where accessing:

http://local.myserver.com/noslashdir

sends me to

http://localhost5/noslashdir/

instead of to

http://local.myserver.com/noslashdir/

I have not been able to override this behavior in the .htaccess file. Here are the relevant details:

/noslashdir/.htaccess:

DirectoryIndex index.html

/.htaccess:

SetEnvIf HTTPS on http_proto=s
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^noslashdir/(.*)$ /noslashdir/$1.html [L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^noslashdir/(.+)\.html$ http%{http_proto}://%{HTTP_HOST}/noslashdir/$1 [L,R=302]

/etc/httpd/httpd.conf

<VirtualHost 127.0.0.5>
    ServerName localhost5
    DocumentRoot /var/www/myserver.com/
    DirectoryIndex  index.php index.html index.htm index.shtml 
    <Directory "/var/www/myserver.com">
            Options Indexes +Includes FollowSymLinks
            AllowOverride All
            Allow from all
            Order allow,deny
    </Directory>
    ScriptAlias /cgi-bin/ "/var/www/myserver.com/cgi-bin/"
</VirtualHost>

/etc/hosts

127.0.0.5   local.myserver.com  localhost5

I have tried RewriteRules, DirectorySlash Off, etc, but nothing has worked so far. All other desired redirects work fine.

Upvotes: 1

Views: 2452

Answers (1)

C3roe
C3roe

Reputation: 96417

Apache is using the ServerName you have set for your VirtualHost when creating “self-referential URLs”.

http://httpd.apache.org/docs/2.2/en/mod/core.html#servername:

“The ServerName directive sets the request scheme, hostname and port that the server uses to identify itself. This is used when creating redirection URLs.”

You should set UseCanonicalName to Off:

“In many situations Apache must construct a self-referential URL -- that is, a URL that refers back to the same server. With UseCanonicalName On Apache will use the hostname and port specified in the ServerName directive to construct the canonical name for the server. This name is used in all self-referential URLs, […]
With UseCanonicalName Off Apache will form self-referential URLs using the hostname and port supplied by the client if any are supplied […]”

Upvotes: 1

Related Questions