0

My .htaccess is

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
RewriteRule . index.php [L]

so all requests i receive in my index.php and then parse query string, and one of my pages is "api documentation page" with url http://domain.com/api so i require page about api from templates

and another url is http://domain.com/api.php where i need receive $_GET['action'] and another data with $_POST

so it will work like http://domain.com/api.php?action=start, but i need call it like http://domain.com/api/start or http://domain.com/api/start/ and receive $_GET['action'] - "start"

but this string in my .htaccess doesnt work

RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA] 
user2368299
  • 369
  • 3
  • 14

3 Answers3

2

You need to keep RewriteCond before every RewriteRule if you want to apply for all URLs or keep them in a separate rule. Try this .htaccess:

Options -MultiViews
RewriteEngine On
RewriteBase /

# ignore all files/directories from all rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^api/(.+)$ api.php?action=$1 [L,NC,QSA]

RewriteRule . index.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

Ah, I've had that problem before, can't remember exactly how it went, but this link seems relevant: https://wiki.apache.org/httpd/RewriteFlags/QSA

The only difference with yours is the slash at the beginning to make the paths absolute and you don't need to escape slashes. E.g.

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^api/([a-z]*)/? api.php?action=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . index.php [L]

But none of those differences seem to be the cause of the problem. As pointed out by nubhava, the RewriteCond's only affect the next rule though. When faced with this kind of situation though, I usually end up using a router, to do the "rewriting" in PHP. See also Mod-Rewrite or PHP router?

Community
  • 1
  • 1
outlyer
  • 3,933
  • 2
  • 14
  • 18
  • it doesn't work and my css, js and images stops work whitch is in http://domain.com/css/reset.css and the same – user2368299 Nov 22 '14 at 15:56
  • 1
    Just to be sure, you kept the RewriteCond's, right? – outlyer Nov 22 '14 at 16:01
  • this is my RewriteCond's: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d – user2368299 Nov 22 '14 at 16:04
  • 1
    Yes, that should be them. I just checked it, turns out at least in my server the opening slash is a problem, but it otherwise works. I'm editing my answer – outlyer Nov 22 '14 at 16:17
0
RewriteEngine On
RewriteBase /

RewriteRule ^api/([^/]*)$ /api.php?action=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

work's, http://htaccess.madewithlove.be/ help's me trying different combinations

user2368299
  • 369
  • 3
  • 14