0

I'm having some issues figuring out how to use an htaccess file. I've got apache/php installed on an ubuntu system and mod_rewrite is turned on (php_info() states that it's in the list of loaded modules). The web server works, displays html and php files, so I'm happy with that.

What I'm trying to figure out now is how to use an htaccess file properly. I created a directory, /data, with an index.php file in it. All I want it to do at the moment is just display the $_REQUEST variable so I can see if things are working the way I assume they should.

Example: If I type in the following URL: localhost/data/info1/ I want the htaccess file to access localhost/data/index.php?request=info1

However, no matter what I enter in to the htaccess file, I keep getting 404 errors, and I'd like to understand why.

Here's my htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule data/(.*)$ data/index.php?request=$1 [L]
</IfModule>

I've made no changes to the config file, to activate mod_rewrite, I used the ubuntu a2enmod command so ubuntu did it for me. After that, I restarted apache.

What I can't figure out is why this doesn't work. My assumption is that there's still some sort of configuration I need to do on the server end, but I honestly don't know what. Is there any advice anyone can offer me?

Lisa
  • 2,102
  • 7
  • 26
  • 44
  • Maybe a basic thing. But i noticed you typed `htaccess` everywhere instead of `.htaccess`, the latter is the correct filename for a htaccess file. (Again, might be obvious, but just making sure here..) – Damien Overeem Jan 11 '14 at 18:28
  • Hi Damien! The file is .htaccess on the server. I apologize for any confusion. – Lisa Jan 11 '14 at 18:31

1 Answers1

1

Here's the fix:

RewriteRule ^data/(.*)$ data/index.php?request=$1 [L]

(You were missing a ^)

EDIT:

In the OP, you have another leading / in the URL example, in this case it'd be:

RewriteRule ^data/(.*)/$ data/index.php?request=$1 [L]
Ryan Brodie
  • 6,554
  • 8
  • 40
  • 57
  • Hi Brodie! I have made the changes you suggested as well as adding the following line: RewriteLog /tmp/rewrite.log Now, the log file doesn't seem to be changing, so that makes me think that there is some additional configuration I need to do as well. – Lisa Jan 11 '14 at 18:47
  • Yes your `.htaccess` may not be loaded or enabled: [here's how to check](http://stackoverflow.com/questions/9234289/verify-if-htaccess-file-is-running). – Ryan Brodie Jan 11 '14 at 18:50
  • As a side note, if you're doing a lot of manual mod rewrites you may want to check out a lightweight routing framework like [Slim](http://www.slimframework.com/). I use it in production and is very stable, if you're stuck with PHP I'd highly recommend it. – Ryan Brodie Jan 11 '14 at 18:52
  • Hi Brodie! Thanks for that! Apparently I didn't enable use of .htaccess files in the apache conf, even though mod_rewrite was turned on. It's all working now, thank you so much! – Lisa Jan 11 '14 at 19:19