mod_rewrite

mod_rewrite

Posted by: zweismith
Posted on: 2005-03-31 23:24:00

Hi all,

Is mod_rewrite supported at the site level on DH? If so, maybe I'm not doing things right. If not, then I need another solution.

I want the following URL:
http://mydomain.com/hello/world/
to resolve to...
http://mydomain.com/script.php?hello=world

"world" in the above example might be any arbitrary string.

I added the following to my .htaccess file in my site's root dir, but no success (404 error):

RewriteEngine On
RewriteRule ^/hello/(.*) /script.php?hello=$1

Thanks in advance for helping out an apache novice!

-Dave


Re: mod_rewrite

Posted by: macmanx
Posted on: 2005-03-31 23:27:00

Unfortunately, I don't know how to do .htaccess rewrite (though there are plenty of tutorials that can be found via a Google search). What I can tell you, however, is that mod_rewrite is support at the site level on DH.

Re: mod_rewrite

Posted by: Atropos7
Posted on: 2005-04-01 00:43:00

It's probably due to the lack of a RewriteBase directive.

Try:

RewriteEngine On
RewriteBase /
RewriteRule ^hello/(.*) /script.php?hello=$1 [QSA]

Now, the RewriteBase directive tells Apache what "virtual directory" we are rewriting for so it can re-build the URL properly. You should note that I have removed the value of RewriteBase from the beginning of the regexp as a result.

I added the QSA flag, you might want to remove it.
With QSA: /hello/world/?version=2 becomes /script.php?hello=world/&version=2
Without QSA: /hello/world/?version=2 becomes /script.php?hello=world/

See http://httpd.apache.org/docs/mod/mod_rewrite.html:
""
When a substitution occurs for a new URL, this module has to re-inject the URL into the server processing. To be able to do this it needs to know what the corresponding URL-prefix or URL-base is. By default this prefix is the corresponding filepath itself. But at most websites URLs are NOT directly related to physical filename paths, so this assumption will usually be wrong! There you have to use the RewriteBase directive to specify the correct URL-prefix.
""
cool Perl / MySQL / HTML CSSEdited by Atropos7 on 04/01/05 00:47 AM (server time).

Re: mod_rewrite

Posted by: zweismith
Posted on: 2005-04-01 01:09:00

That's it exactly! I had tried adding "RewriteBase /" since my original post, but not from the regexp. Did as you suggest and all is working well.

Thanks for your help!

-Dave

Tags: hello worldscript phpmydomainhtaccess filemodhttproot dirnoviceresolveapachesuccessurl