Forum Moderators: phranque

Message Too Old, No Replies

Apache rewrite rule help please

Rewriting dynamic to static URL

         

cassandra

10:26 pm on Dec 8, 2003 (gmt 0)

10+ Year Member



Hi,

I need to redirect a cgi url to a static page.

For example /dummy.cgi?ID=123 it should redirect to
/dummy/d123.html

I have tried using this rule but it fails.

RewriteEngine on
Options +FollowSymlinks
RewriteBase /cgi-bin/
RewriteRule ^"\(dummy.cgi?ID=\)"(.*) /dummy/d$2.html [R=301,L]

What I am I doing wrong.

Thanks.

jdMorgan

1:29 am on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cassandra,

Welcome to WebmasterWorld [webmasterworld.com]!

> For example /dummy.cgi?ID=123 should redirect to /dummy/d123.html


Options +FollowSymlinks
RewriteEngine on
RewriteBase /cgi-bin/
RewriteCond %{QUERY_STRING} ^ID=(.*)$
RewriteRule ^dummy\.cgi$ /dummy/d%1.html [L]

The order of the directives is important -- Options, Engine, Base.

I should point out here that it is far more usual to rewrite static URLs to dynamic ones in order to call scripts. The scripts then output static URLs for visitors and search engine spiders. However, the code modification above should get you closer to what is needed to do what you asked for.

Introduction to mod_rewrite [webmasterworld.com]

Jim

cassandra

4:12 am on Dec 9, 2003 (gmt 0)

10+ Year Member



Thank you Jim

The mod is working but with a small problem,

Instead of rewriting the url /dummy/d123.html
It is writing as /dummy/d123.html?ID=123

Your help is appreciated.

Thanks.

jdMorgan

4:21 am on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops! - Sorry, add a "?" to the rule to clear the query string:

RewriteRule ^dummy\.cgi$ /dummy/d%1.html[b]?[/b] [L]

cassandra

4:56 am on Dec 9, 2003 (gmt 0)

10+ Year Member



Its working great,

We are using this redirection because we have moved from cgi to static pages and lots of our cgi urls are indexed in the search engines.

So we wanted a permanent redirection.

Can you tell us what is the load impact on the server with this rewrite rule.

Thanks once again.

jdMorgan

5:29 am on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Permanent external redirection (to tell search engines to change their listings of your pages):

RewriteRule ^dummy\.cgi$ [b]http:[i][/i]//www.example.com[/b]/dummy/d%1.html? [[b]R=301,[/b]L]

Server load? Probably negligible. If you want to absolutely minimize the load, and you have access to the server configuration, move the rewrite code to httpd.conf. For use in httpd.conf, you'll have to change it slightly by adding a leading slash to the pattern:

RewriteRule ^[b]/[/b]dummy\.cgi$ http:[i][/i]//www.example.com/dummy/d%1.html? [R=301,L]

Jim

cassandra

5:43 am on Dec 9, 2003 (gmt 0)

10+ Year Member



Wonderful,

Thank you for all your help.