• Resolved Matt Pierce

    (@mausmalone)


    I’m setting up a test WordPress server on my local machine (which is running stock Apache, MySQL, and PHP). I started completely from scratch with a new directory, new database, and new virtual host. The WordPress installation went smoothly, pretty links work fine, and I was able to set up my network using subdirectories.

    However, I’ve run into some problems with redirects that stem from one our IT mandates. Our IT department requires that we don’t allow .htaccess files anywhere on the server. All configuration changes must be done in the httpd.conf file by IT.

    Currently in my httpd.conf file, I have these blocks:

    #WordPress sites
    <Directory ~ "c:/Sites/wp_[0-9a-zA-Z-_]+">
    	# BEGIN WordPress
    	<IfModule mod_rewrite.c>
    	RewriteEngine On
    	RewriteBase /
    	RewriteRule ^index\.php$ - [L]
    	RewriteCond %{REQUEST_FILENAME} !-f
    	RewriteCond %{REQUEST_FILENAME} !-d
    	RewriteRule . /index.php [L]
    	</IfModule>
    	# END WordPress
    </Directory>
    
    #WordPress network sites
    <Directory ~ "c:/Sites/wpn_[^/]+/">
    	<IFModule mod_rewrite.c>
    	RewriteEngine On
    	RewriteBase /
    	RewriteRule ^index\.php$ - [L]
    
    	# add a trailing slash to /wp-admin
    	RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
    
    	RewriteCond %{REQUEST_FILENAME} -f [OR]
    	RewriteCond %{REQUEST_FILENAME} -d
    	RewriteRule ^ - [L]
    	RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
    	RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
    	RewriteRule . index.php [L]
    	</IfModule>
    </Directory>

    However when I try to go to a dashboard for one of the network’s sub-sites I get stuck in a circular redirect. (https://network-test.localhost.me/arts/wp-admin redirects to https://network-test.localhost.me/arts/wp-admin )

    So here’s the head scratcher – If I change the above block to allow .htaccess and then put the mod_rewrite options into a .htaccess file, everything works fine. I just can’t do that because my IT department has disallowed the practice.

    I’m self-taught in Apache and there are a ton of things I don’t fully grasp, so does anybody know why these settings would work in .htaccess but wouldn’t work in httpd.conf?

Viewing 4 replies - 1 through 4 (of 4 total)
  • have you tried using the network configuration only?
    i suggest using that one only

    Thread Starter Matt Pierce

    (@mausmalone)

    That’s not how that works. Apache selects which one to use based on the name of the directory. The two don’t get used at the same time.

    Thread Starter Matt Pierce

    (@mausmalone)

    Hooray, I’ve got it but it took some time. The problem is that Apache’s mod_rewrite module works differently depending on if it’s invoked from .htaccess, httpd.conf, and (importantly) a Directory statement within an httpd.conf file.

    My solution here was to move the mod_rewrite commands out of the Directory block and into the VirtualHost block for my site. I lose the directory matching convenience, but hey – it’s working now. I also had to make some modifications because mod_rewrite from within a httpd.conf file expects leading slashes. Here’s the code (plus comments in case you ever wondered what all this stuff does).

    <VirtualHost network-test.localhost.me>
    	DocumentRoot "c:/Sites/wpn_network-test"
    	ServerName network-test.localhost.me
    
    	RewriteEngine On
    
    	# If the visitor requested index.php, just end here
    	RewriteRule ^index\.php$ - [L]
    
    	# add a trailing slash to /wp-admin
    	RewriteRule ^/([_0-9a-zA-Z-]+/)?wp-admin$ /$1wp-admin/ [R=301,L]
    
    	# If the visitor requested an existing file or directory, serve
    	# the asset like normal
    	RewriteCond %{REQUEST_FILENAME} -f [OR]
    	RewriteCond %{REQUEST_FILENAME} -d
    	RewriteRule ^ - [L]
    
    	# If the visitor requested a resource in one of the wordpress directories
    	# from within a subsite, silently give them the one from the network
    	# wordpress directory
    	RewriteRule ^/([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 [L]
    
    	# If the visitor requested a PHP file in the subsite, silently route
    	# request to the network's PHP file (license.php, wp-config.php, etc ...)
    	RewriteRule ^/([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 [L]
    
    	# If all else fails, allow index.php to handle the error condition.
    	RewriteRule . /index.php [L]
    </VirtualHost>
    Thread Starter Matt Pierce

    (@mausmalone)

    UPDATE: This didn’t work when I went to production replica because our server admin has 2 virtual hosts (one for HTTPS and one for HTTP). On the upside I did figure out a working solution.

    It turns out that Apache completely mangles URLs passed to mod_rewrite if the RewriteRule is in a Directory block that matches to a wildcard or regular expression. If you match to a specific directory then it behaves as expected. So now my Apache configuration looks like this:

    NameVirtualHost network-test.localhost.me
    <VirtualHost network-test.localhost.me>
    	DocumentRoot "c:/Sites/wpn_network-test"
    	ServerName network-test.localhost.me
    </VirtualHost>
    
    # WordPress network sites - each has to be defined separately.
    # WARNING - Using a regular expression Directory will cause unexpected behavior
    # 			in mod_rewrite. DO NOT consolidate these.
    <Directory "c:/Sites/wpn_network-test">
    	RewriteEngine On
    	RewriteBase /
    
    	# If the visitor requested index.php, just end here
    	RewriteRule ^index\.php$ - [L]
    
    	# add a trailing slash to /wp-admin
    	RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ /$1wp-admin/ [R=301,L]
    
    	# If the visitor requested an existing file or directory, serve
    	# the asset like normal
    	RewriteCond %{REQUEST_FILENAME} -f [OR]
    	RewriteCond %{REQUEST_FILENAME} -d
    	RewriteRule ^ - [L]
    
    	# If the visitor requested a resource in one of the wordpress directories
    	# from within a subsite, silently give them the one from the network
    	# wordpress directory
    	RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 [L]
    
    	# If the visitor requested a PHP file in the subsite, silently route
    	# request to the network's PHP file (license.php, wp-config.php, etc ...)
    	RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 [L]
    
    	# If all else fails, allow index.php to handle the error condition.
    	RewriteRule . /index.php [L]
    </Directory>

    This means a little more work for me when creating a new test site, but testing and production both work.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Help with configuring network rewrites using httpd.conf instead of .htaccess’ is closed to new replies.