#6133: Getting mod_rewrite to work for CakePHP in Subdirectory ---------------------------+------------------------------------------------ Reporter: Louis | Type: Enhancement Status: new | Priority: Medium Milestone: 1.2.x.x | Component: Routing/Dispatcher Version: 1.2 Final | Severity: Normal Keywords: | Php_version: PHP 4 >= 4.3.2 Cake_version: 1.2.1.8004 | ---------------------------+------------------------------------------------ Hello all,
I'm trying to set up a default installation of CakePHP in a subdirectory (by username) on a shared host where I've no access or control to server logs. It has been hit-or-miss so far, but I've greatly appreciated the Apache documentation. Setting up the paths inside the webroot directory was simple, and while I can then use /index.php/ format URLs, I would much prefer using mod_rewrite to eliminate the index.php entirely. I kept getting 500 errors until I finally read the documentation on mod_rewrite with a fine-toothed comb. Here's what I discovered: Note: Never forget that Pattern is applied to a complete URL in per- server configuration files. '''But in per-directory configuration files, the per-directory prefix (which always is the same for a specific directory!) is automatically removed for the pattern matching and automatically added after the substitution has been done.''' This feature is essential for many sorts of rewriting, because without this prefix stripping you have to match the parent directory which is not always possible. There is one exception: If a substitution string starts with http:// then the directory prefix will not be added and an external redirect or proxy throughput (if flag P is used!) is forced! Note: To enable the rewriting engine for per-directory configuration files you need to set ``RewriteEngine On'' in these files '''and ``Options FollowSymLinks'' must be enabled'''. If your administrator has disabled override of FollowSymLinks for a user's directory, then you cannot use the rewriting engine. This restriction is needed for security reasons. The parts in bold are what I really have to highlight here as important and ideally, taken into consideration within CakePHP. What fixed the mod_rewrite perfectly for me was to add '''Options +FollowSymLinks''' above '''RewriteEngine on'''. The trick to know if you need the above '''Options''' line? Comment out everything except '''RewriteEngine'''. Then change it to '''off'''. Your site should load. Change it to '''on'''. You've a 500 error! Add the '''Options''' line, with '''Engine on'''. Poof, no more 500! So now I've everything working -- except -- the URL string is not being processed by CakePHP correctly. Why? As noted in the above quotation from the Apache 1x docs, in subdirectory rewriting, the subdirectory is AUTOMATICALLY added to the $1 in url=$1. Which means that when it is interpreted by CakePHP, Cake has to know that the first part is the subdirectory. This can be done by comparison, automatically, however my directory is actually ~username, which can also be interpreted as %7Eusername, and so could fail if that isn't taken into account. (Assuming this is the current behavior, as what I'm seeing now when loading / is Missing Controller: '%7EusernameController' unless I type /index or /test.) Edit: Forget the last paragraph. I just now tried typing something like /~username/home and it gave a 500 error. I then added to the .htaccess, '''RewriteBase /~username''' and now going to /~username/home says, Error: HomeController could not be found. So now my only problem is the default / as it still says %7EusernameController. How can I get that to work like going directly to index.php or '''index.php?url=/'''? I just tried playing with .htaccess for the last half hour, and nothing seems to work from there to fix this / problem. So I resorted to the following in app/config/routes.php: {{{ Router::connect('%7Eusername/', array('controller' => 'pages', 'action' => 'display', 'home')); Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); }}} And now it all works, though it feels a little hack-ish. In closing, here's my .htaccess file that seems to work perfectly, except for the / issue: {{{ <IfModule mod_rewrite.c> Options +FollowSymLinks RewriteEngine on RewriteBase /~username RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] </IfModule> }}} Oh and I'm submitting this as an Enhancement because I really think deployment in a subdirectory is a use-case to consider, bit it could also just be a clarification for existing documentation... Having said all this, I love how I can set up CakePHP to have the webroot files in /~username/MyWebSite, the host-enforced web directory, and with a little permissions tooling, put the app, cake and vendor files under /~username, and then have it all work! Here were my modifications to webroot's index.php file: {{{ if (!defined('ROOT')) { define('ROOT', dirname(dirname(__FILE__))); } }}} Yep, just making it look one directory up was all it took after chmodding, and yet I feel so much safer! :-) For the curious, this is a website hosted at yorku.ca, where accounts are centrally created and managed, with aliasing. So in FTP, you upload files to ~/username/MyWebSite with that exact capitalization, and troubleshooting PHP or doing Apache configuration without logs is absolutely painful. On the external end, the URL is both yorku.ca/~username as well as yorku.ca/username, which causes a bit of confusion when it comes to routing, let me tell you. I think I'm probably the first person to even attempt to (or successfully) use mod_rewrite on any yorku.ca site that isn't set up by the central web team. Prior to this, I tried using Joomla, but even their .htaccess file neglects to mention Options, though it does have RewriteBase. Ah the joys of PHP deployment. And here I thought Rails was hard ... -- Ticket URL: <https://trac.cakephp.org/ticket/6133> CakePHP : The Rapid Development Framework for PHP <https://trac.cakephp.org/> Cake is a rapid development framework for PHP which uses commonly known design patterns like ActiveRecord, Association Data Mapping, Front Controller and MVC. Our primary goal is to provide a structured framework that enables PHP users at all levels to rapidly develop robust web applications, without any loss to flexibility. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "tickets cakephp" group. To post to this group, send email to tickets-cakephp@googlegroups.com To unsubscribe from this group, send email to tickets-cakephp+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/tickets-cakephp?hl=en -~----------~----~----~----~------~----~------~--~---