I have a website, which is a one-pager JS-based site. It has different "subsites" and "categories", but since everything is controlled via JS, and since there are about 30 subsites and 50 categories, I have never created a "normal" subsite system, just like
www.example.com/subsite/category
Instead I have only the main site
www.example.com
and that's all, everything else is controlled via JS.
But I want to achieve better results on Google ranking, and for that I need to create subpages as well.
I want to keep the JS-based behavior, and that part is ready to handle the different URLs (www.example.com/subsite/category) the right way: it is checking the URL, takes the subsite and the category, and passes to the right JS as parameters. So my one-pager site acts like a multi-pager. And it's fine in this way.
At this point my .htaccess redirects all non-existing directories to the home page, keeping the URL itself unchanged, so the JS can use it properly.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.html [L]
But I want to handle the non-existing subsites and categories as well, I want to 404 them. And I need to handle it via .htaccess.
So I thought maybe there is a way to handle the existing subsites and categories as variables in .htaccess, where all of their combinations are accepted, but all other goes to 404.
For example (in JS, since I don't know how to handle this in .htaccess):
var subsitesArray = ["foosite", "barsite"];
var categoriesArray = ["foocategory", "barcategory"];
So the valid URLs would be:
www.example.com/foosite/foocategory
www.example.com/foosite/barcategory
www.example.com/barsite/foocategory
www.example.com/barsite/barcategory
And all other would be non-valid, so 404.
If I would have to set all URLs manually, that would be 30*50 URLs... That's way too much.
Is it possible to solve it somehow in .htaccess?
UPDATE#3 Please update the code to support the following points:
- The
/site1.../site30,/category1.../category50subsites are available on the server (index.html in these directories), so the .htaccess rules should not forward them to index.html (but let the "physical" files to be opened). - So only
/site1/category1.../site1/category50.../site30/category1.../site30/category50variants should be redirected to index.html. www.example.com/////site1///category2(so a lot of/characters in-between) are still accepted, however should not be.- When the link ends with a
/character, it's not accepted, however it should be.www.example.com/site1/categoryis accepted, butwww.example.com/site1/category/(ending with/) is not, however it should be.
Can you please update the code? These would be the final modifications, and it would work perfectly.
Thank you in advance.