The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Redirecting to Slim app in subdirectory

Derek Hauffe's Avatar

Derek Hauffe

03 Oct, 2013 03:51 PM

I have a web directory structure like so:

root
    /content
        /plugins
            /myplugin
                /Slim (directory containing Slim Framework)
                index.php
    /other_directory_1
    /other_directory_2
    .htaccess
    index.html

I'm having difficulty getting a successful redirect or server configuration in order to redirect to a nonexistent directory, and serve the Slim app as though it were actually installed in that nonexistent directory.

Here are a few example URLs, which I'd like to be able to use in the browser's location bar, or link to in documents or apps.

1. http://example.com/nonexistent_dir
2. http://example.com/nonexistent_dir/info
3. http://example.com/nonexistent_dir/info/details

I'm trying to rewrite these URLs so that they actually make requests from the Slim app (with index.php in the /myplugin directory) as follows:

1. http://example.com/content/plugins/myplugin/index.php
2. http://example.com/content/plugins/myplugin/index.php/info
3. http://example.com/content/plugins/myplugin/index.php/info/details

It's important the apparent URLs remain as they appear in the first example, without being changed in the location bar.

Here's what is currently in the .htaccess file in the root directory:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^nonexistent_dir(/.*)?$ /content/plugins/myplugin/index.php$1 [L]
</IfModule>

When I enter http://example.com/nonexistent_dir in the location bar, I'm redirected to the / route in Slim. However, adding any additional routes (e.g., http://example.com/nonexistent_dir/info) results in me getting a No input file specified. message.

My server is apache, with mod_rewrite enabled, in a shared hosting environment. If anyone can offer some tips to getting this routing sorted out, it would be greatly appreciated. Thanks!

Derek

  1. 1 Posted by tonycastle on 03 Oct, 2013 04:32 PM

    tonycastle's Avatar

    What is your document_root currently set to? I presume it is the directory you describe as root in your directory structure description?

    If you create your index.php file in the root directory and run your slim app from there, the actual directory structure is irrelevant at that point as slim takes over the routing at the application level as apache routes everything through index.php due to your htaccess file.

    Also, it's not a good idea to use the structure you describe as the slim library and your application code will be publicly accessible.

  2. 2 Posted by Derek Hauffe on 03 Oct, 2013 04:40 PM

    Derek Hauffe's Avatar

    Thanks for your quick response, tonycastle.

    Being on a shared hosting server, I do not have access to the apache config files (I'm assuming you refer to httpd-vhosts.conf in this case).

    I understand that putting index.php in the site root would do the job, but there are several other aspects of the site in addition to just the slim app, so that is not an option for me.

    The site definitely allows me to use .htaccess.

  3. 3 Posted by Derek Hauffe on 03 Oct, 2013 04:47 PM

    Derek Hauffe's Avatar

    Looks like you changes your response while I was typing mine.

    I'm not sure what the danger is in having the app code accessible (despite not being referenced by any links, I understand that it's in the "public" area of the site). Database access credentials are stored in a Wordpress database table, so they aren't in the app folders. Seeing the app code will let the curious know that I am in fact accessing a database, but so far as I understand it won't be a security risk.

  4. 4 Posted by Derek Hauffe on 03 Oct, 2013 05:13 PM

    Derek Hauffe's Avatar

    I can relocate the app code to an area above the web root, but that still doesn't address the desire to point to index.php in a subdirectory. Unfortunately, it sounds like this is beyond the scope of the support you can offer. I'll keep at it in other venues. Thanks for your time.

  5. 5 Posted by tonycastle on 03 Oct, 2013 06:37 PM

    tonycastle's Avatar

    are you trying to add a rewrite rule for each specific "non-existant" directory rather than a general rewrite rule that sends all traffic to this domain to your index.php page?

    If so, is there any reason for that?

  6. 6 Posted by Derek Hauffe on 03 Oct, 2013 06:57 PM

    Derek Hauffe's Avatar

    No, not at all.

    I'm trying to redirect
    `http://example.com/nonexistent_dir[/anything_else]`

    to

    `http://example.com/content/plugins/myplugin/index.php[/anything_else]`

    and have it all processed by the Slim app located at `http://example.com/content/plugins/myplugin/index.php`.

    The `/anything_else` should be treated by Slim as routing info, instead of being treated by the the server as a pointer to an actual file. It's important that the redirect not overwrite `http://example.com/nonexistent_dir[/anything_else]` to look like `http://example.com/content/plugins/myplugin/index.php[anything_else]`.

  7. 7 Posted by Derek Hauffe on 03 Oct, 2013 07:01 PM

    Derek Hauffe's Avatar

    I may not have been clear. When I use the phrase `nonexistent_dir`, I'm referring to a consistent URL, which will always appear in the location bar as `http://example.com/nonexistent_dir`. The value of nonexistent_dir will not change depending on the incoming request, nor will the fact that I'm trying to redirect it and all subsequent route information to `http://example.com/content/plugins/myplugin/index.php` (while still preserving any additional routing info).

  8. 8 Posted by tonycastle on 03 Oct, 2013 07:06 PM

    tonycastle's Avatar

    so what exactly is the nonexistent_dir for? So what is stopping you from using the basic rewrite all rule for the domain:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ /content/plugins/myplugin/index.php [QSA,L]

  9. 9 Posted by tonycastle on 03 Oct, 2013 07:11 PM

    tonycastle's Avatar

    ah - I see, you can do this within slim. you can add a hook before the router matches the route which removed the directory name from the path_info in the request. The router will then match the route based on the remaining "anything_else" in the path_info. You can then use another hook after the router has matched to add the directory name back onto the start of the path_info. This means that the urlFor() function in Slim will build urls for your links with the correct url structure.

  10. 10 Posted by Derek Hauffe on 03 Oct, 2013 07:19 PM

    Derek Hauffe's Avatar

    That sounds like it's just what I'm looking for. So I'll take a look at the docs for the urlFor() function. If you're aware of anything relevant that might assist me in my research, I'll welcome links to the info. Thanks for your help, Tony.

  11. 11 Posted by Derek Hauffe on 03 Oct, 2013 07:28 PM

    Derek Hauffe's Avatar

    Responding to your question in comment #9 ("so what exactly...")

    There is an existing website in place, which has many pages of information and an established directory structure. It's not an option to put the Slim app at `example.com/index.php`. But, rather than requiring `example.com/content/plugins/myplugin/index.php`, I'd prefer to allow `example.com/nonexistent_dir`.

    This means that I can't redirect everything to the `/content/plugins/myplugin/index.php` path, because that would break the serving of all of the rest of the website. I need a conditional redirect, one which takes _only_ `http://example.com/nonexistent_dir[/anything_else]` and redirects to `http://example.com/content/plugins/myplugin/index.php[/anything_else]`, while still allowing the rest of the website to function as normal.

  12. 12 Posted by Derek Hauffe on 03 Oct, 2013 07:37 PM

    Derek Hauffe's Avatar

    Reading the docs on urlFor(), it looks like this is not quite what I need. As I understand it, urlFor() allows one to take a named route and output the corresponding url. If I have a functioning, named `/info/details` route (named "details"), and I want to generate a link to it, I can use urlFor() to output the url that will take me there. So long as the name remains the "details", even if the actual route becomes `/info/about/details`, when I request the url for the details page, I can use urlFor() and arrive in the proper location.

    My problem occurs before getting to this point. I'm only hitting the `/` route. Every other route results in a `No input file specified.` message.

  13. 13 Posted by Derek Hauffe on 03 Oct, 2013 07:44 PM

    Derek Hauffe's Avatar

    I believe that my problem lies more with the configuration of the apache server than with any shortcoming of the Slim framework. I was just hoping that someone here had encountered a similar challenge and could share their knowledge. While I welcome continued assistance with this challenge, I appreciate that it's probably outside of the scope of your interest, Tony. I will continue to seek answers elsewhere, and if I'm unable to find one I'll change my goals.

  14. 14 Posted by Abhishek Deb on 16 Mar, 2014 07:18 PM

    Abhishek Deb's Avatar

    Hi, infact I ran into the exact problem. Still I am unable to find any answer. Its simply ignoring the trailer part. Also, I found out that if you give a trailer something as /abc.xyz , it routes to 404 error. bdw, my rewite is this
    `
    RewriteEngine On
    RewriteBase /AppName/

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^v1(/.*)$ API/v1/index.php$1 [NC,L]

    `

    I blv, the problem is in (/.*) part.

  15. 15 Posted by johnk on 19 Mar, 2018 10:12 AM

    johnk's Avatar

    I think your Apache config needs to have the AcceptPathInfo directive set.

    http://riceball.com

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac