How make htaccess for single-page app within a sub

2020-07-18 08:20发布

问题:

I want to have a single-page app, but only within a sub URL like:

//The following would all be valid, but handled by the "index.html" 
//at http://www.test.com/webapp
http://www.test.com/webapp
http://www.test.com/webapp/settings
http://www.test.com/webapp/start

I want all the URLs to look exactly like above, but be handled by: http://www.test.com/webapp/index.html.

I also want the following URLs to not be handled by that page:

http://www.test.com
http://www.test.com/contact
http://www.test.com/about

How do I do this?

REGULAR SINGLE_PAGE HTACCESS

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.html [QA,L]
</ifModule>

回答1:

Just adding answer for reference.

The commnet from @Croises:

"The same (with [QSA] not [QA]) but in the webapp directory"

Worked for me.

The ".htaccess" must be on the subdieretory.

Example working ".htaccess":

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.html [QSA,L]
</ifModule>

The only diference is "[QA,L]" to "[QSA,L]"



回答2:

The answer marked as good didn't work for me. Below my solution:

1) Create .htaccess in the folder where you place your app
2) Paste the code below
3) Change folder_name to your folder name.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /folder_name/
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /folder_name/index.html [L]
</IfModule>