Forum Moderators: phranque

Message Too Old, No Replies

Can't seem to use forward slashes in RewriteRule

         

androidtech

2:42 am on Aug 3, 2006 (gmt 0)

10+ Year Member



Here's a sample htaccess file I'm experimenting with:

RewriteEngine on
RewriteRule ^.*hello.html$ [YOURDOMAIN.com...] [L]

Echo.php is simple document that says "Hey you rewrote the URL!".

If I try the following URL on my server:

[YOURDOMAIN.com...]

It works as expected. Now I want to try matching sub-directories in the incoming URL. I tried this:

RewriteEngine on
RewriteRule ^.*/test/hello.html$ [YOURDOMAIN.com...] [L]

It no longer matches. I get a 404 error instead. I tested the RewriteRule regular expression in Kodos, a regular expression tester, and it worked fine. But for some reason, it doesn't parse when I try it on my server. I've tried other variations, and as soon as a I try putting a forward slash in the test part of a RewriteRule, it no longer matches.

What am I doing wrong?

Thanks.

jdMorgan

3:02 pm on Aug 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Be very very sure of the local URL-path you use in your patterns: You've started your patterns with ".*/" -- Which means, "Match anything, and as much as possible, followed by a slash." Unless you actually need that function, don't use it.

But the local URL-path seen by RewriteRule in an .htaccess context is always stripped of its leading slash, so your subdirectory rule will never work as expected -- The leading slash in your pattern will never be present in the local ULR-path derived from your request if the code is in .htaccess.

You should also escape any literal characters that will otherwise be treated as regular-expressions tokens, "." for example.

To rewrite a request for "http://www.example.com/test/hello.html", in .htaccess, try:


RewriteEngine on
RewriteRule ^test/hello\.html$ http://www.example.com/test/echo.php [L]

Jim

androidtech

4:34 pm on Aug 3, 2006 (gmt 0)

10+ Year Member



jdMorgan,

Thanks, it was indeed the stripped forward slash that was the problem.

Is there any way to get status messages from mod_rewrite on a shared host?