Forum Moderators: phranque

Message Too Old, No Replies

What am I doing wrong

Newbie .htaccess problem

         

StanTheMan

1:48 pm on May 18, 2004 (gmt 0)

10+ Year Member



My .htaccess file looks like this
<<

RewriteEngine On
RewriteRule ^(.*)_bar.html foo.php?input=$1

>>

I would like it to work so that foo.php?input=movement can be read as movement_bar.html

Its not working where about am I going wrong?

Thanks for your help.

Birdman

2:00 pm on May 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I see two problems:

1) You must escape(\.) any periods since they are used in regular expression matching.

2) You need to start the rewritten URL with a slash.

RewriteRule ^(.*)_bar\.html$ /foo.php?input=$1

Birdman

PS: You can also add the end anchor($), since you know there will be nothing after the .html.

StanTheMan

2:17 pm on May 18, 2004 (gmt 0)

10+ Year Member



Thats excellent Birdman, wonderful you have saved my hours of frustration.

Thanks

Birdman

2:51 pm on May 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Stan, glad it helped. Here is a good mod_rewrite tutorial [httpd.apache.org] for future reference.

Birdman

gergoe

3:04 pm on May 18, 2004 (gmt 0)

10+ Year Member



Try putting
Options +FollowSymLinks
in your htaccess file, if is it not there already. You need to enable symbolic links in order to have the mod_rewrite working properly.
If this does not help, try a very simple rule like this one:

RewriteRule ^silly_file\.html [i]real_file[/i].html

Change the real_file.html to some file which exists in the directory where this htaccess file resides.

The second (missing) slash in your RewriteRule is not a problem since in the htaccess files only the "local" path appears, any directory reference from the url which is above the current one is stripped off, and unless you use fully qualified url (starting with [)...] for the substitution string, the stripped part will be added back after the last rule. So you don't need to worry about that. But this means that if the /abc (virtual) directory is an alias for the /xyz directory, then the mod_rewrite should know that the url which needs to be added in front of the rewritten url is not /xyz but /abc, then you have to use the RewriteBase directive to "fix" this. See the mod_rewrite documentation [httpd.apache.org] for further details.