Mod Rewrite

I've been working on an application which needs Mod Rewrite. It seems to not be working. I did check the httpd.conf file in the httpd directory to ensure that

LoadModule rewrite_module libexec/httpd/mod_rewrite.so

was not commented out. I'm also assuming that httpd.conf is the configuration file being used.

To test I created two simple web page with a .htaccess file that redirects from one to the other.

RewriteEngine On
RewriteRule ^alice.html$ bob.html

Am I missing something?

IMac, Mac OS X (10.5)

Posted on Dec 6, 2007 1:47 PM

Reply
15 replies

Dec 6, 2007 2:34 PM in response to charlie-imac

What a timely post! I just finished debugging a problem I was having with mod rewrite. Try adding the following just before your rewrite section:
Options +FollowSymlinks

If your cgis stop working (as mine did) add the same line to that section.

Look for something like the following in your error log:
<pre style="* {margin: 0; padding: 0;} overflow: auto;font-size:small; font-family: Monaco, 'Courier New', Courier, monospace; color: #222; background: #ddd; font-size: 10px;">Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive i
s forbidden</pre>

--
Cole

Dec 6, 2007 3:55 PM in response to charlie-imac

options +FollowSymlinks may not need to be enabled, depending on whether or not it is on by default from the httpd.conf file.

A few more hints:

Don't "copy and paste" text from webpages, as it contains invisible non-UNIX characters causing the rule to fail (or a server error).

Examine the error_log in Console while you are loading pages, it often reports errors you never knew existed.

Dec 6, 2007 6:18 PM in response to charlie-imac

Your problem is quite simple - your rule will never, ever match an incoming request, and therefore will never invoke the rewrite.

The rule:

<pre class=command>RewriteRule ^alice.html$ bob.html</pre>

tells Apache to serve the page 'bob.html' whenever a request for "alice.html" comes in.

The problem is that you will never get a request for "alice.html" because all HTTP requests will at least start with a trailing /

Therefore, a more likely scenario would be:

<pre class=command>RewriteRule ^/alice.html$ bob.html</pre>

which invokes the rewrite when "/alice.html" (a valid HTTP request) comes in.

Dec 7, 2007 8:08 AM in response to charlie-imac

Thanks for the responses. I have added

Options +FollowSymlinks

and changed the rule to

RewriteRule ^/alice.html$ bob.html

but it has had no effect. I am using the Console application to view the log file and I only see this error which is being generated when I view the page from Dreamweaver.

12/7/07 11:02:36 AM [0x0-0x25025].com.adobe.dreamweaver-9.0[259] JS Error:
12/7/07 11:02:36 AM [0x0-0x25025].com.adobe.dreamweaver-9.0[259] missing ; before statement
12/7/07 11:02:36 AM [0x0-0x25025].com.adobe.dreamweaver-9.0[259] filename: Macintosh HD:Library:WebServer:Documents:eryx1:test mod rewrite:.htaccess
12/7/07 11:02:36 AM [0x0-0x25025].com.adobe.dreamweaver-9.0[259] lineno: 2
12/7/07 11:02:36 AM [0x0-0x25025].com.adobe.dreamweaver-9.0[259] line: RewriteEngine on
12/7/07 11:02:36 AM [0x0-0x25025].com.adobe.dreamweaver-9.0[259] at: on

Is there a different error log that I should be looking at? I did view a page with phpinfo(); and see that Loaded Modules - mod_rewrite is being loaded.

What am I still missing?

Dec 7, 2007 11:15 AM in response to charlie-imac

Well I still haven't gotten mod rewrite to work I have found the following:

httpd -V tells me that the config file is / -D SERVER CONFIGFILE="/private/etc/apache2/httpd.conf"

The config file does have mod rewrite enable. I also changed AllowOverride from None to All and restarted the server. The log files are at /var/log/apache2/

I can write a nonsense rule which will generate a error to the browser window and the error_log file.
Now what I can't seem to do is write a simple rule to test mod rewrite. I did add the / to my rewrite test file.

Any other suggestions? I've spent the morning on this and still can't seem to get it working or verify that it is working.

Dec 7, 2007 12:34 PM in response to charlie-imac

I still think the Options is a red herring, but the fact you can get some rewrites to work is encouraging.

The next step is to turn on rewrite logging. Try adding something like:

RewriteLog /var/log/apache2/rewrite_log
RewriteLogLevel 3

to your httpd.conf and hit the page that should generate the redirect. Post the results if you need help deciphering them. You can increase the RewriteLogLevel number (up to 9) to create more verbose logging, but note that these logs can get very large, so you don't want to run the RewriteLog any longer than necessary.

Dec 7, 2007 2:53 PM in response to Cole Tierney

I think you just need to add the [R].


Adding the R will send a redirect back to the client - essentially saying 'the file /alice.html has been moved to bob.html (although in this case the redirect needs to be a full URL, so 'RewriteRule ^/alice.html$ http://some.server.net/bob.html' would be more correct).

If that's what the OP wants, fair enough, but without the [R] apache would silently serve the page 'bob.html' in place of 'alice.html' without the user noticing any difference.

Dec 7, 2007 5:32 PM in response to Camelot

{quote}If that's what the OP wants, fair enough, but without the [R] apache would silently serve the page 'bob.html' in place of 'alice.html' without the user noticing any difference.{quote}

Wow! I had no idea mod rewrite could do this. All the examples I've looked at involved redirects, so I falsely assumed that was the only way it worked. Thank you, Camelot!

So I did some testing and discovered that the original RewriteRule ^alice.html$ bob.html does work on my machine. And that was without RewriteBase /. In addition ^/alice.html$ bob.html did not work.

So the mystery is still unsolved, but I'm grateful to learn something new about mod rewrite.
--
Cole

Dec 8, 2007 2:35 PM in response to Cole Tierney

In addition ^/alice.html$ bob.html did not work


Oops, my bad.

I was so focussed on the fact that the search term needed a leading / I didn't notice that the rewrite term didn't have one, either. The URI that you rewrite to also needs a leading /, so:

<pre class=command>RewriteRule ^/alice.html$ /bob.html</pre>

will work - and will serve the file /bob.html anytime the user requests /alice.html.
The rest still applies - appending [R] at the end will send a redirect to the client.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Mod Rewrite

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.