ExampleEngineering
ExampleEngineering

Reputation: 35

HTACCESS - How to make RewriteCond that will "cut" URL address

Lastly I started development API for my web application. I want to make mine API look like those "famous" APIs like Discord one - good looking and easy-to-use. This is my folder hierarchy:

root:
   api:
      api.php
      .htaccess

I'd like to make RewriteCond in .htaccess file that will make link like that:

https://example.com/api/user/1234/test

Into something like that:

https://example.com/api/api.php?input=user/1234/test

I am new into .htaccess, but I have read some docs, but

Thanks for help in advantage.

EDIT 1

My current .htaccess file only contains links to ErrorDocuments.

Upvotes: 0

Views: 46

Answers (1)

phparkle
phparkle

Reputation: 86

First off, make sure your .htaccess is being read by Apache. AllowOverride all should appear in the directory block of your virtual host config:

<VirtualHost *:80>
    <Directory /path/to/web/root>
        AllowOverride all
    </Directory>
</VirtualHost>

Then, add these lines to your .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(api/v2/)(.*)
RewriteRule .* %1api.php?input=%2

This introduction to mod_rewrite from the apache docs is very helpful: https://httpd.apache.org/docs/current/rewrite/intro.html#regex

You can play around with different rules here: https://htaccess.madewithlove.be?share=853262c5-e0a5-5048-96bc-684aec5da2fb

Upvotes: 1

Related Questions