2

Do I need to use .htaccess file for a multisite? I have setup basic wordpress site and configured it with apache2 VirtualHost as:

<VirtualHost *:80>
    ...
    RewriteEngine On
    Options FollowSymLinks

    # THIS REDIRECTS TO HTTPS
    RewriteCond %{SERVER_NAME} =example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

    DocumentRoot /var/www/example/
    <Directory /var/www/example/>
       Options Indexes FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
    </Directory>
</VirtualHost>
<VirtualHost *:443>
    ...
    DocumentRoot /var/www/example/
    <Directory /var/www/example/>
       Options Indexes FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
    </Directory>

    SSLEngine on

    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>

However, when I follow the steps to enable multisite, wordpress prompts me to paste these options to my .htaccess file:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]

But I do not have an .htaccess file. However If I make it, and configure apache, rewrite rules do not work, e.g. example.com/wp-admin is not rewritten into wp-admin/ with a trailing slash, but into ugly long link: https://example.com/wp-login.php?redirect_to=https%3A%2F%2Fexample.com%2Fwp-admin%2F&reauth=1. Can this be because of SSL rewrite rules? My questions are: do I need .htaccess? And how do I make its rewrite rules work?

  • If you are using Apache and even have AllowOverride active, what is the problem with using a .htaccess file? – kero Mar 20 '18 at 14:51
  • I just want to have everything in one file if possible. But even then, when I configure VHost with AccessFileName .htaccess I do not see that link to wp-admin is rewritten. – Liliane Mar 20 '18 at 14:59
  • 1
    That admin link is the same one that's given even if pretty urls are on. Why would it matter if an admin URL (Which isn't indexed by search engines anyway) is not pretty? You can paste those rules into your virtualhosts config if you don't want a .htaccess file but you'll find your life a whole lot easier if you allow for one. – Steve North Mar 20 '18 at 15:57
  • @SteveNorth yes, I have been reading, it's easier to have one. I will just include SSL redirection in .htaccess. – Liliane Mar 20 '18 at 16:20
  • Are you hosting multiple domains with this VirtualHost - if not then your redirection to HTTPS should be simplified to use a mod_alias Redirect instead. Also, are you really on Apache 2.2? – MrWhite Mar 20 '18 at 17:25
3

e.g. wpinstance.com/wp-admin is not rewritten into wp-admin/ with a trailing slash, but into ugly long link: https://wpinstance.com/wp-login.php?redirect_to=https%3A%2F%2Fwpinstance.com%2Fwp-admin%2F&reauth=1

As is already mentioned in comments, this is intentional. There is no "pretty" URL for this. And neither does there need to be - this should not be indexed or linked to.

do I need .htaccess?

No, you don't "need" .htaccess.

If you have access to the server config, then you can paste these directives verbatim inside the <Directory /var/www/wpinstance/> container (inside the <VirtualHost *:443> container).

However, to paste those directives directly inside the <VirtualHost> container you would need to make several changes, since mod_rewrite behaves differently in a server context (as opposed to a directory or .htaccess context).

Note that everytime you make changes to the server config then you will need to restart Apache. You don't need to restart Apache when you change the .htaccess file.

I would also consider disabling MultiViews - unless you specifically require this for something? ie. Remove MultiViews from the Options directive. (MultiViews can conflict with mod_rewrite - depends what you are doing.)

If you are not going to use .htaccess files then they should be disabled:

AllowOverride None

UPDATE:

I'm using Apache/2.4.18

Note that the Order and Allow directives are deprecated in Apache 2.4, and will be removed in future versions. You need to change the following (Apache 2.2 and earlier) directives:

Order allow,deny
Allow from all

to read:

Require all granted

in Apache 2.4+

  • Thank you very much on the advices, especially the MultiVIews. I've decided to keep .htaccess because it seems easier. I have moved https redirection from <VirtualHost> into .htaccess also. – Liliane Mar 21 '18 at 9:22
  • Although my domain does not get rewritten from wpinstance.com to wpinstance.com/index.php as the rules of .htaccess imply. – Liliane Mar 21 '18 at 9:26
  • Is your site not working? Your .htaccess rules don't actually perform that specific rewrite (from the document root) - note the single dot (.) regex. This "rewrite" is left for mod_dir instead (mod_dir will issue a subrequest for the directory index by default). – MrWhite Mar 21 '18 at 9:36
  • Sorry, I used wpinstance instead of example.com, but it turned out wpinstance.com exists, and I'm not the owner of it. Yes my site works. Thank fot that info, it seems that I don't yet quite understand how htaccess and rewrite work. But redirection to https works from htaccess, so I guess everything works just fine :) – Liliane Mar 21 '18 at 9:51
  • The internal "rewrite" to index.php is entirely server-side (internal to the server) - there is no change to the URL the user sees in the browser (unlike a "redirect"), if that is what you were expecting. (Yes, always best to use example.com for example URLs. You can still edit this into your question. Although I hadn't tried to access wpinstance.com.) – MrWhite Mar 21 '18 at 10:09

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.