1

My WP moved from localhost to online, and now it shows a blank page (wp-admin is working normally). This is the structure: sub.domain.nl/dir1/dir2/WP-install

Something must me off with the redirecting I think after trying these: - removing all plugins - activate default template - change functions.php - turned on debug mode - played around with wp-config and .htaccess

codex.wordpress wasn't much helpful either as it, like most posts about this, wants me to place files (like .htaccess) in the root of the site.

The WP install should only be living in the sub.domain.com/dir1/dir2/WP-install directory. Because there are other pages above: sub.domain.nl/dir1/dir2 (like a 'site menu').

The .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?sub.domain.nl$
RewriteCond %{REQUEST_URI} !^/dir1/dir2/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dir1/dir2/$1
RewriteCond %{HTTP_HOST} ^(www.)?sub.domain.nl$
RewriteRule ^(/)?$ dir1/dir2/index.php [L] 
</IfModule>

Another try at the .htaccess (this does display the site, but all the links get duplicated like: https://www.sub.domain.nl/sub.domain.nl/dir1/dir2/index.php)

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sub.domain.nl/dir1/dir2/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /sub.domain.nl/dir1/dir2/index.php [L]
</IfModule>
# END WordPress

Update

I changed the RewriteBase and RewriteRule to:

RewriteBase /internship/avans/
RewriteRule . /internship/avans/index.php [L]

Frontpage gets displayed correctly, wp-admin work like before, and some page/post links work ?!? Some get the following error (also after clearing cookies):

Not Found The requested URL /sub.domain.nl/dir1/dir2/index.php was not found on this server.

I removed those pages and reposted them, they work fine now. No idea what this irregular behavior causes. I checked my DB for wrong updates links as sub.domain.nl/sub.domain.nl within phpMyadmin and with Search/Replace, but that seemed not to be the case.

Anyhow, it now works- but I am still wondering what caused some links to work and some of then to reproduce the double links and consequently a Not Found error.

Update 2

I ended up using the suggested .htaccess by @MrWhite down below: Read his post for elaboration!

# 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
  • If wp-admin is working without problem, you could try deleting the htaccessfile and then let WP create it again for you by saving the permalink settings again. – Antti Koskinen Feb 16 at 8:51
  • I tried that: the funny thing is that when I go to permalink settings, delete .htaccess and click update I get kicked out to the login page: sub.domain.nl/dir1/dir2/… – Ludo Feb 16 at 8:55
  • That might have something to do with the WP login cookie. When your cookie (logged in session) and WP stuff don't match anymore, you get kicked out. The same thing happens when you change site url / wp url from http to https. But, does the site work now properly? – Antti Koskinen Feb 16 at 9:03
  • No it does not work, I cant get to re-saving the permalink options. And I also cleared my cookies, log in, do these steps.. – Ludo Feb 16 at 9:27
  • 1
    It doesn't. It only shows the front page. The links that go to the doubled URL do not work. – Ludo Feb 16 at 11:34
1

If WordPress is installed in the directory /dir1/dir2 and the corresponding .htaccess file is also in that subdirectory, ie. /dir1/dir2/.htaccess, then the .htaccess file should look something like:

# 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

This is very similar to the default WordPress .htaccess file, except I have commented out (removed) the RewriteBase directive and removed the slash prefix on the RewriteRule substitution (ie. what was /index.php is now index.php). This is important, as it allows Apache to add back the directory-prefix (the location of the .htaccess file) at the end of processing.

This obviously means that your URLs must contain the /dir1/dir2 directory prefix.


The two .htaccess files you posted don't work because...

RewriteCond %{HTTP_HOST} ^(www.)?sub.domain.nl$
RewriteCond %{REQUEST_URI} !^/dir1/dir2/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dir1/dir2/$1
RewriteCond %{HTTP_HOST} ^(www.)?sub.domain.nl$
RewriteRule ^(/)?$ dir1/dir2/index.php [L]

This is intended to be placed in the document root /.htaccess file with the intention of hiding the /dir1/dir2 subdirectory from the URL. By placing this in /dir1/dir2/.htaccess it just won't do anything since the condition that checks the REQUEST_URI server variable will fail and the RewriteRule pattern ^(/)?$ will never match.

RewriteBase /sub.domain.nl/dir1/dir2/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /sub.domain.nl/dir1/dir2/index.php [L]

This would work if /sub.domain.nl was a directory on the filesystem (in the URL space), but it's not; it's the hostname, so it doesn't make much sense. This allows the front page (homepage) to be displayed because the first RewriteRule prevents further processing. However, sub pages will likely result in an Apache generated 404 as /sub.domain.nl/dir1/dir2/index.php does not exist.

Note that, in this example, the RewriteBase directive does not do anything. The assigned RewriteBase only applies to relative path substitutions (ie. not absolute and not root-relative - starting with a slash). There are no relative path substitutions in this example.


UPDATE:

I changed the RewriteBase and RewriteRule to:

RewriteBase /internship/avans/
RewriteRule . /internship/avans/index.php [L]

The net result with my version (at the top) is the same. However, the RewriteBase directive is redundant and hardcoding the URL-path in this way is superfluous and less portable.

  • The RewriteBase /internship/avans/ directive is redundant in this example (and could be removed), since you have included the full URL-path in the RewriteRule substitution. The RewriteBase is only used when you have a relative path substitution (as mentioned above) in order to override the directory-prefix. So, to use the RewriteBase in this example you would write this as:

    RewriteBase /internship/avans/
    RewriteRule . index.php [L]
    
  • Hardcoding the URL-path naturally makes it less portable.

  • You only need to include the RewriteBase OR specify the full URL-path in the RewriteRule substitution (you don't need to do both) if the .htaccess file is not in the application root directory of your WordPress install. eg. If WordPress is installed in /dir1/dir2, but the .htaccess is in the document root (in order to hide the WordPress application directory).


Bit of an aside, but... for the reasons mentioned above, the "default" WordPress .htaccess file is incorrect/misleading IMO:

# 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

The RewriteBase / is not used here since the URL-path (simply /) is explicitly included in the RewriteRule substitution: /index.php. You don't need both, and this seems to create a lot of confusion for users. In fact, you don't need either if the .htaccess file is in the application root (which it is most of the time). By "application root" I'm referring to the directory where WordPress is installed, either the document root, or a subdirectory.

IMO, the "default" WordPress .htaccess file should be the one I've modified at the top of my answer (comment out the RewriteBase directive and remove the slash prefix) - which is what you are now using. This is far more portable and "just works" without modification regardless of where WordPress is installed (and you probably wouldn't have needed to have asked your question, since the default file would have "just worked").

  • As you can read in my update (1) I got it to work. Thank you so much for you elaboration. Could you please explain why my solution or your solution if preferable? – Ludo Feb 16 at 22:44
  • Glad you got it working - you're welcome. I've updated my answer with more explanation comparing the two versions. – MrWhite Feb 17 at 1:27

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.