1

I'm trying to get an API working (locally, for now), which lives in a subfolder. I've been trying every example I can find, but all are ending up in 404 errors.

I have this URL:
http://127.0.0.1/~owner/personal/api/v1/index.php/tasks

I want to be able to use this:
http://127.0.0.1/~owner/personal/api/v1/tasks

Eventually, it will become this:
http://api.mydomain.com/tasks

I just can't seem to get my .htaccess rules setup correctly. This keeps getting suggested, but does nothing:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

What am I missing?


Update

I have setup my virtual hosts to use http://api.local/ instead of the previously used addresses.

Accessing http://api.local/index.php/tasks works.

Accessing http://api.local/tasks does not.

0

2 Answers 2

1

Maybe part of your problem is your development environment. You are using a local server to do this on with several subfolders deep and I think it's messing with your .htaccess depending on the location.

If your .htaccess is in the root of your website http://127.0.0.1/.htaccess then it's not going to work properly.

Make sure it's in the same directory as your index.php file and make sure /~owner/personal/api/v1/ is your document root for your dev environment as specified in your apache config file.

Then you can test out your htaccess rules and see how they work. Your current rule should be ok providing /tasks won't be a real directory in the root.

On another note,

I always suggest using your real domain name so you could see how it works as if it were in production. You can do that by modifying your HOST file on your PC and the site will only available to you and you can access via yoursite.com. This quick easy guide will show you.

Try specifying the rewrite base.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Sign up to request clarification or add additional context in comments.

5 Comments

I don't know why this didn't occur to me earlier. I have setup a virtual host on my machine, and am now pointing at api.local instead of the address being used earlier. I am still unable to get the rewrite rules to work, though, and the api is insisting on having the /index.php/ in there. The /v1 folder is set as root, and the .htaccess file is in there. No luck, though.
I just noticed the first part of your answer... Yes, it would end up being http://api.local/.htaccess... How does that change the rewrite rules in this case?
Hmm, It shouldn't be needed but maybe try specifying the rewritebase. See my example in the update.
Thank you, it's finally working. .htaccess has always been a pain for me. I'm glad you were able to help nail this down.
Been there before. No problem.
0

You are using the parameters of RewriteRule backwards. You need this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*?)/index.php(.+)?$ /$1$2

This means:

  • if it's not a file
  • and it's not a directory
  • and it looks like / (optional), possibly some more characters, then /index.php, then possibly some more characters, go to the URL without the /index.php part.

Note that I deleted the [L] for now -- it means "if the URL matches this rule, don't apply any more rules." You say you want to keep transforming it into http://api.mydomain.com/tasks, so you shouldn't use [L].

1 Comment

This seemed promising, but didn't work, unfortunately

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.