Forum Moderators: phranque

Message Too Old, No Replies

Getting started with mod rewrite

         

Qure

2:17 pm on May 25, 2009 (gmt 0)

10+ Year Member



Hello there,

I'm trying to mirror our website's set-up locally so I can work on it, and the only thing I'm struggling with is the mod_rewrite module for Apache.

I've followed instructions on many pages found through Google, but nothing seems to work - I set up this very basic example so I can verify when it is working:

workingwith.me.uk/articles/scripting/mod_rewrite

...however, Alice's page never redirects to Bob's page. I'm using the basic .htaccess described in that tutorial, which is in my site's root directory along with alice.html and bob.html.

RewriteEngine on
RewriteRule ^alice.html$ bob.html

I used the command "--enable-rewrite=shared" when configuring Apache before compiling and installing. A search for "mod_rewrite" in my Apache directory turns up files, so I can safely assume it's installed correctly.

There wasn't anything in my httpd.conf file mentioning mod_rewrite though, which I found strange given some of the instructions online which say it should be there, so I added:

LoadModule rewrite_module modules/mod_rewrite.so

...but this isn't making any difference. I don't see anything of note coming out in the logs either.

Is there a major step I could be missing? I suspect it won't read my .htaccess file or doesn't see it - is there any reason this could happen?

Thanks in advance for any advice...

EDIT: I forgot to mention this is Apache 2 I am using

[edited by: Qure at 2:43 pm (utc) on May 25, 2009]

jdMorgan

6:05 pm on May 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In your server config file (httpd.conf or other), either put "Options +FollowSymLinks" in the <Directory> container for this site, or set "AllowOverride FileInfo" at a minimum. If you choose to do the latter, then include "Options +FollowSymLinks" in your .htaccess file -- It must be present at some level in order to use mod_rewrite.

Both the requirement for the FileInfo override and FollowSymLinks are documented in the mod_rewrite documentation.

Your syntax is for an internal rewrite -- a URL-to-filename translation. If you want to redirect the client with a URL-to-URL translation, the syntax is different.

Internal Rewrite:

 RewriteRule requested-url-path /local-filepath [L] 

External (client) Redirect:

 RewriteRule requested-old-url-path http://example.com/new-url-path [R=301,L] 

A rewrite is used primarily when you change the path to the file, but keep the same URL. For example, it is used to map search-engine-friendly URLs to scripts, passing the 'pieces' of the friendly URL to the script as 'GET' variables in a query string.

A redirect is used primarily when you change a URL on your pages, and need to inform clients and search engines that are still following old links form other sites that the requested resource has moved.

There are of course many other applications for these functions. Make sure you understand what you really need to do before proceeding further -- We see many Webmasters doing external redirects when what is needed are internal rewrites: You don't want to ever change a URL unless a judge or a lawyer says you must.

Jim

Qure

4:33 pm on Jun 7, 2009 (gmt 0)

10+ Year Member



Thanks for the response - I've recently been trying again with this, and I'm not convinced mod_rewrite is enabled on my Apache.

I've installed Apache so much times I've lost count. I'm using official instructions for installing Apache 2.0, except when I get to the "Configure" stage before compiling, I specify "--enable-module=so" (I need this for Resin) and "--enable-module=rewrite". However, when I use "httpd -l", I never see mod_rewrite in the list. mod_so is there however.

Many instructions online tell me to "uncomment LoadModule rewrite..." in my httpd.conf file, as well as a couple of other lines related to the module - but these aren't present in the file. I added the "LoadModule rewrite_module modules/mod_rewrite.so" myself, but this just says:

Cannot load /usr/local/apache2/modules/mod_rewrite.so into server: /usr/local/apache2/modules/mod_rewrite.so: cannot open shared object file: No such file or directory

What is it I'm doing wrong? Why isn't the module in that directory like the others?

jdMorgan

9:09 pm on Jun 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cannot load /usr/local/apache2/modules/mod_rewrite.so into server: /usr/local/apache2/modules/mod_rewrite.so: cannot open shared object file: No such file or directory

Clearly, mod_rewrite.so does not exist at the path you have specified.

Jim