I am trying to create a Single Page App that works this way:
If the HTTP Request header Accept is set to application/json server service.php else server index.html
Here is my current Config File:
<Location "/" >
RewriteEngine on
# JSON response
RewriteCond %{HTTP_ACCEPT} application/json [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . service.php [L]
# HTML Response
RewriteCond %{HTTP_ACCEPT} !application/json [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.html [L]
</Location>
I also need to handle paths("/") for SPA
Some of the methods I have tried from Googling around give a 500/400 response code and I have tried to skeem through the Docs whole day.
Please check my configuration and point me to the right direction. Also if posible exaplain briefly how mod_rewrite works. I am using a linux computer.
This code should work for you in your site root .htaccess:
RewriteEngine on
# ignore all files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# JSON response
RewriteCond %{HTTP_ACCEPT} application/json [NC]
RewriteRule . service.php [L]
# HTML Response
RewriteRule . index.html [L]
I wrote mine this way and placed it in VertualHost context:
RewriteEngine on
# JSON
RewriteCond %{HTTP_ACCEPT} application/json [NC]
RewriteRule . /service.php [L]
# Everything else
# Note: for some reason, if the code below is not in Location directive, js & css are loaded with text/html MIME
<Location "/" >
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.html [L]
</Location>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.