0

I'm currently redirecting everything to app/index.html because I have a client-side single page app. However, I want to skip redirecting for actual files and directories. I've gotten this far. However, when I add in the RewriteCond for skipping files that are directories, it counts the document root as well. This causes a visit to a document root such as hitting http://localhost:8080 to just list my dir contents. How would I get this to not apply to the document root or any other better way? P.S. I'm also skipping the /api dir.

Here is what I have so far:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/api
RewriteRule ^(.*)$ /app/index.html/$1 [NC]

So essentially I guess what I'm asking is: How can I skip directories except for the document root directory?

Solution

With help from the answer that Krist van Besien gave I was able to come to a solution.

# not a directory
RewriteCond %{REQUEST_FILENAME} !-d [OR]
# or if it is the document root
RewriteCond %{REQUEST_URI} ^/$
# not a file
RewriteCond %{REQUEST_FILENAME} !-f
# not /api
RewriteCond %{REQUEST_URI} !^/api$
RewriteRule ^(.*)$ portal/index.html/$1 [NC]
1
  • Instead of updating your question to add the solution, you should better post an answer with the solution, remove it from your question and accept your own answer. It's better for the community :)
    – j0k
    Mar 28, 2013 at 8:57

2 Answers 2

2

You could add a RewriteCond:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/api
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ /app/index.html/$1 [NC]
1

I would suggest changing this line:

RewriteRule ^(.*)$ /app/index.html/$1 [NC]

To this; note the ^ is removed:

RewriteRule (.*)$ /app/index.html/$1 [NC]
2
  • Out of curiosity, why does removing the beginning of line ^ matter? Is that to take account for whether there is a leading / or not based on if it is in a .htaccess file versus a vhost or similar?
    – Mike Haas
    Mar 27, 2013 at 13:53
  • Yes, exactly as you describe. Mar 27, 2013 at 14:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .