Redirect with .htaccess if url contains string

1

I'm working on an application using Angular4 and PHP , so that the URL of the Angle works, I need to use a certain setting in .htaccess to point to index.html , like this:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /
    RewriteRule ^index.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
</IfModule>

So far, everything works as it should, I can access and refresh on any page without any problems. However, I also want to use the routing system in PHP , so I need another type of configuration to direct the calls to the index.php , which is in another folder. The project directory looks something like this:

raiz
│ index.html
│ vendor.bundle.js    
| [outros arquivos .js]  
│
└─api
│  │ index.php
│  │ init.php
│  │
│  └─vendor
└─assets
└─[outros arquivos]

And this is the setting to make the% path system work:

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . api/index.php [L]
</IfModule>

The problem is that I can not get both of them working at the same time. Or the route works in PHP or Angular , but not both together.

The routes in PHP need to be directed to the file PHP only when I call some url that contains the string /api/index.php , that is, always have the prefix /api/'alguma-coisa' .

How can I fix this problem and use both routing systems?

... or, if there is another solution, I can use it quietly as long as I reach the end goal.

    
asked by anonymous 01.09.2017 / 21:41

1 answer

0

I do not have very advanced knowledge in .htaccess , but I was able to resolve the issue by changing only 2 lines. I removed RewriteRule ^index.html$ - [L] and moved this part RewriteBase / to the top. With a final code like this:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    RewriteCond %{REQUEST_URI} api 
    RewriteRule . api/index.php [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
</IfModule>

But I will leave the question still open if someone has a better or more educational answer on the subject.

    
02.09.2017 / 01:33