Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Bostonaholic

macrumors 6502
Original poster
Aug 21, 2009
439
0
Columbus, Ohio
I'm trying to implement clean urls for my website. I'd like to use the mod_rewrite module to do this. I have created a .htaccess file to do so. When I upload the file to my hosted webserver, it works just fine but when testing locally with MAMP, it does not. I have searched all around the web and I can't find a solution.

Here's my configuration:

/Users/matthew/Sites/matthewboston/.htaccess
Code:
<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteBase /
	RewriteRule ^/about/?$ ./about.php [QSA,L]
	RewriteRule ^/entry/([0-9]+)/?$ /index.php?id=$1 [QSA,L]
</IfModule>
/Applications/MAMP/conf/apache/http.conf
Code:
...
LoadModule rewrite_module modules/mod_rewrite.so
...
<Directory "/Users/matthew/Sites/matthewboston">
        Options All
        AllowOverride All
        Order allow,deny
        Allow from all
</Directory>
...
AccessFileName .htaccess
...

I think that's all the important info...

Help please!!
 
I don't know how much it matters but I just noticed something. In my htaccess file it is checking <IfModule mod_rewrite.c> but in http.conf it uses mod_rewrite.so

I removed the 'If' statement but that didn't fix the problem. Would this cause issues anyway?
 
Syntax-wise it looks alright. In my .htaccess I don't use the IfModule tags. You can try removing that tag (and end tag).

What type of results are you currently get?
 
Ok, so I know MAMP's apache is reading the .htaccess file cause I added
Code:
ErrorDocument 404 /404.php
and it correctly displays 404.php for missing pages. So it just seems to be the RewriteRule's that aren't working properly...
 
For the about line, that period is like causing problems so it can be removed.
Code:
RewriteRule ^/about/?$ /about.php [QSA,L]
 
For the about line, that period is like causing problems so it can be removed.
Code:
RewriteRule ^/about/?$ /about.php [QSA,L]

I thought that too. I changed it and it still didn't help.
Code:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^/about/?$ /about.php [QSA,L]
RewriteRule ^/entry/([0-9]+)\.php$ /index.php?id=$1 [QSA,L]

ErrorDocument 404 /404.php
 
Does the entry line work locally? You may want to try swapping the two lines (about and entry) to see if it has any effect. Also, when you try things in your browser locally, does it show what URL it's attempting to create, after the rewrite? Just trying to figure out if the rewrite line is simply not triggering when it should, or if they rewrite is not writing to the correct format. It's weird though that it works on your site.

One quick thing that just came to mind. When you edited the httpd.conf file, did you restart the web server? It needs to be restarted when editing that file. Editing the .htaccess file does not though.
 
Does the entry line work locally? You may want to try swapping the two lines (about and entry) to see if it has any effect. Also, when you try things in your browser locally, does it show what URL it's attempting to create, after the rewrite? Just trying to figure out if the rewrite line is simply not triggering when it should, or if they rewrite is not writing to the correct format. It's weird though that it works on your site.

One quick thing that just came to mind. When you edited the httpd.conf file, did you restart the web server? It needs to be restarted when editing that file. Editing the .htaccess file does not though.

Nope, the entry line doesn't work either. When the rewrite doesn't work I get a 404 and the url in the browser reads http://localhost:8888/about/.

I never edited the httpd.conf file, by default it had those settings. And these issues have existed after multiple restarts of the server.

I appreciate your help, angelwatt. You, SrWebDeveloper and savar are great!
 
Grrrrrrr, I finally figured it out.

I need to use this
Code:
RewriteRule ^about/?$ /about.php [QSA,L]
RewriteRule ^entry/([0-9]+)\.php$ /index.php?id=$1 [QSA,L]

It was the / in front of the about and entry in the regular expression. I was reading this page - http://corz.org/serv/tricks/htaccess2.php - and about halfway down it mentions
Remember, if you put these rules in the main server conf file (usually httpd.conf) rather than an .htaccess file, you'll need to use ^/... ... instead of ^... ... at the beginning of the RewriteRule line, in other words, add a slash.
So then I took out the first slash and now it works. I still don't know why it worked on my hosted site (GoDaddy) and not locally using MAMP. Oh well. Thanks angelwatt!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.