问题
I'm building a small app in ReactJS, so all pages need to serve index.html and the JS handles the url. This works fine. But I'd also like to have .htaccess remove www from the url if it exists. I'm reading through the mod_rewrite documentation and I can't quite figure out how to make it do both.
Here is my code in .htaccess, please advise!
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC]
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
回答1:
Answered my own question
<IfModule mod_rewrite.c>
RewriteEngine On
# remove www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L,NE]
# redirect all to index
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [L,NC]
</IfModule>
来源:https://stackoverflow.com/questions/39396488/react-router-redirect-to-index-html-and-remove-www-from-url-in-htaccess