2

For a web-application I would like to rewrite the URL.

From:

http://example.com/api/logger/all

To:

http://example.com/api/index.php/logger/all

So I tried the following:

RewriteEngine On
RewriteCond %{REQUEST_URI} !/index.php/  
RewriteRule ^api(.*)$ api/index.php$1 [L]

I also testet it successfully with http://htaccess.madewithlove.be/.

On the server I get a 404.

My project struct looks like following:

--api
----index.php
--htaccess
--index.html

Update:

I found a solution for external redirects.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !(index\.php)
RewriteRule ^api/(.*)$ /api/index.php/$1 [R=302]

But I need a internal redirect and if remove [R=302] I get 404 with "No input file specified." as response. Following htaccess file works for http://example.com/logger/all.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !(index\.php)
RewriteRule ^(.*)$ /api/index.php/$1

But adding api again results in "No input file specified."

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !(index\.php)
RewriteRule ^api(.*)$ /api/index.php/$1
2
  • 1
    That looks okay, so what's the problem now.? Commented Dec 23, 2015 at 19:23
  • What is generating the "No input file specified." error? That isn't an Apache error, so it would seem to be your script that is generating that error? Your last ruleset would result in a double slash - is that a problem for your script? You should either change ^api(.*)$ to ^api/(.*)$ (your redirect "that works" contains the slash), or remove the slash in the substitution. But you should probably also use the L flag. Commented Dec 24, 2015 at 12:19

1 Answer 1

3

mod_rewrite is a double-edged sword that is incredibly easy to misuse.

Apache 2.2.16 and later provide the extremely useful but surprisingly seldom used FallbackResource directive.

So use the following layout:

--api
----.htaccess
----index.php
--index.html

And use the following contents for your .htaccess file:

FallbackResource /api/index.php

The main difference with the mod_rewrite based solutions is that there is no substitution in the URI, so you have to parse the URI in PHP using $_SERVER['REQUEST_URI'].

For example here, if you access http://example.com/api/logger/all, your index.php file will see the following value:

echo $_SERVER['REQUEST_URI']; // prints /api/logger/all

The main interest of putting the .htaccess file in the api directory is that it ensures that only URLs under the /api prefix are handled by FallbackResource. Any URL that would cause a 404 error in the document root folder will not trigger a call to /api/index.php.

Sign up to request clarification or add additional context in comments.

1 Comment

The only problem is that by adding this FallbackResource /api/index.php, the content of the index gets printed on the page if accessed like this: /api/index

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.