Creating .htaccess for Vue.js website with prerendering

Viewed 1434

I have .htaccess file with content below (it's slightly modified example from vue-router documentation):

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]

  RewriteCond %{HTTP_HOST} ^www.example.com$
  RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
</IfModule>

I want to:

  • redirect from www. to non-www (this is in in the above code and works)
  • add a rule to remove trailing slashes (so, a server should redirect from http://example.com/ to http://example.com)
  • redirect URLs like http://example.com/? to http://example.com

I tried to remove trailing slashes by:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

But server falls into redirect loop (301 to 302, 302 to 301, and so on) and I don't have any idea how to fix this.

The website is based on Vue.js with prerendering, so if it has http://example.com/contact page, the server should render file from /contact/index.html directory.

0 Answers
Related