Forum Moderators: phranque

Message Too Old, No Replies

Clean URL project

         

redox

12:00 pm on May 6, 2012 (gmt 0)

10+ Year Member



Hello,

Content is currently served by a PERL script, e.g.:
http://www.example.com/cgi-bin/script.pl?main_group=group2&sub1=topic

I recoded the script in PHP and in an effort for nice URLs I plan to use this rewrite rules in the .htaccess:

# Enable clean URLs like http://www.example.com/group2/topic
RewriteRule ^(group1|group2|group3)/([-a-zA-Z0-9_]+)$ /script.php?main_group=$1&sub1=$2 [L]

# REDIRECT FOR OLD PERL URLs
RewriteCond %{QUERY_STRING} ^main_group=([-a-zA-Z0-9_]+)&sub1=([-a-zA-Z0-9_]+)$ [NC]
RewriteRule ^cgi-bin/script\.pl$ http://www.example.com/%1/%2? [R=301,L]


While the first rewrite is working I'm not able to make the 301 Redirect work for the old URLs.

Forgive my newbie questions, once per year I get confronted with mod_rewrite and each time I realize I forgot what I learned the year before.
Thanks for your help.
redox

g1smd

5:27 pm on May 6, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can't redirect requests for cgi-bin (as far as I know).

The cgi handler usually grabs the request long before mod_rewrite is invoked.

lucy24

9:19 pm on May 6, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Aside: This group

[-a-zA-Z0-9_]+

looks as if it can be expressed as

[^/.]+ or [^=&]+

(depending on whether it's in the query string or the body of the URL) at a savings of eight bytes per occurrence :) You may want to use * instead of + depending on exactly what form your requests could take. If so, make sure your script has a response for null inputs.

redox

8:38 am on May 9, 2012 (gmt 0)

10+ Year Member



Hello,

@g1smd
Thank you very much. Here is what I meanwhile observed:
Obviously the rules don't work in the .htaccess of my development server. However on the production server I have root access and the rules work when I place them within the vhost.conf and restart the apache. So in this case the action seems to take place before the cgi handler gets involved.

@lucy24
Thanks for the tipps!

Oliver :-)

g1smd

8:44 am on May 9, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for the observation that setting this in vhost.conf allows correct operation.

This might vary on different versions of Apache but it's a possibility for many people.