4

I'm running a PHP app on the Heroku cedar stack. Is there a way to set up URL rewrite rules? I had hoped to do this in .htaccess files, but only access control directives are recognized in this.

I see that the Ruby advice is to use rake rewrite, but what needs to happen for a PHP app have URL rewrite configuration on Heroku?

Tim Trampedach
  • 201
  • 2
  • 6
  • Where do you see you can't use URL rewriting on heroku ? All documentation I find on the internet about PHP running on heroku makes URL rewriting usable. Eg, http://bitbetter.se/2011/11/11/wordpress-on-heroku/ – Damien MATHIEU Jun 04 '13 at 07:58
  • 1
    I did a ton of trial and error around this and it turns out the reason I thought URL rewrites weren't working is because the interpretation of the .htaccess files was different between my Mac and heroku. Something with how the regex for string replacements is interpreted is different, though I was never able to exactly figure out what or why. I now maintain a heroku and a Mac portion in my .htaccess files to work around it and have moved on from trying to properly solve it. – Tim Trampedach Jun 04 '13 at 18:57

2 Answers2

7

You can use a normal .htaccess file with PHP on Heroku. Simply create a .htaccess file with the re-write conditions, similar to this:

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L]
</IfModule>

This example lifted from WordPress. I believe all urls now redirect to /index.php. Example here: https://github.com/catsby/php-heroku-htaccess and app here: http://php-heroku-htaccess.herokuapp.com/something

catsby
  • 11,276
  • 3
  • 37
  • 37
0

So to partially answer my own question and continue what I alluded to in the comments, here's the difference between a Mac and a heroku .htaccess file that I needed to make:

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ $1/$2?arg=$3 #Mac
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ $1/$2.php?arg=$3 #heroku

This is more of a workaround than a solution. I believe the apache versions are different, but in the end I just capitulated on debugging this further and use a .htaccess file with two sections in it that I comment/uncomment.

Tim Trampedach
  • 201
  • 2
  • 6