phpapachemod-rewriteurl-rewritingpretty-urls

htaccess url rewriting, ignoring files


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.


Solution

  • 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]