1

I am trying to come up with a rewrite rule, but I am having problems.

What I need - any url that starts with /services/XXX to be redirected to /services/api.php?service=XXX

I also want to ignore any files or folders that might also match.

What I have so far:

RewriteRule ^services/([a-z]+)$ /services/api.php?service=$1 [NC, L]

But this does not work at all, it shows a 404 page saying that file is missing when I test it. Any help is much appreciated.

YemSalat
  • 19,986
  • 13
  • 44
  • 51

1 Answers1

1

To ignore the files that exists just ignore the "RewriteCond"s.

Just rewrite everything that comes into services/ to api.php?service ...

Put this into your services folder.

Be careful with your encoding, save the file as .htaccess with a encode that your server reads.

# .htaccess mod_rewrite

RewriteEngine On

RewriteRule ^(.*)$ api.php?service=$1 [QSA,L]

If you don't need to avoid Files, then just go to:

# .htaccess mod_rewrite

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.*)$ api.php?service=$1 [QSA,L]
KodornaRocks
  • 425
  • 3
  • 14
  • 1
    That works! Thanks so much! If I only want to avoid files (but not directories and links), I just remove this line: `RewriteCond %{REQUEST_FILENAME} !-f` from you second example, right? – YemSalat Apr 21 '15 at 04:03
  • 1
    Yeap! -d = Folder -f = File -s = File not empty (it's cool but doesn't work in windows server don't know why) -l Symbolic link -F File accessible (via subrequest)) -U URL accessible (via subrequest)) – KodornaRocks Apr 22 '15 at 05:18