Welcome to WebmasterWorld Guest from 54.145.209.107

Forum Moderators: Ocean10000 & incrediBILL & phranque

Using Mod_Rewrite for the first time

Dynamic URL Rewrite with .htacess

   
9:14 pm on Mar 19, 2003 (gmt 0)

10+ Year Member



I just converted my static web site to dynamic using MySQL and Perl. I need to use mod_rewrite to direct my pages for better spidering.

This is what I have:

rewriteEngine on
rewriteRule ^pages/(.*)$ /index.cgi?page=$1 [T=application/x-httpd-cgi,L]
rewriteRule ^hostingplan/(.*)$ /index.cgi?page=$1 [T=application/x-httpd-cgi,L]

my pages are located at:
myweb.com/pages/Contact_Us.htm
myweb.com/hostingplan/Mini_Plan.htm

End result should look like this:
h*tp://myweb.com/index.cgi?page=Contact_Us.htm

From researching this forum, I was able to come up with the above rules. I have a few questions, if someone could help I would be grateful.

1. Does my code look okay? Do I need to change anything?

2. I am not sure exactly what the [T=application/x-httpd-cgi,L] means. If someone could explain, I would appreciate it. Do I need to use it?

3. I have tried to redirect my old links to the above new links using Redirect Permanent. But it wouldn't work. Any suggestions?

Thanks in advance for any help. I appreciate the advice.
Rose

9:28 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this. I removed the ",L" from the first RewriteRule [httpd.apache.org]

rewriteEngine on
rewriteRule ^pages/(.*)$ /index.cgi?page=$1 [T=application/x-httpd-cgi]
rewriteRule ^hostingplan/(.*)$ /index.cgi?page=$1 [T=application/x-httpd-cgi,L]

9:30 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Rose,

Looks OK to me. You can test this by modifying one of the old "page names" and then trying to access that "page name" directly using your browser. For example, add


RewriteRule ^testpages/(.*)$ /index.cgi?page=$1 [T=application/x-httpd-cgi,L]

to your .htaccess file, and then type [yourdomain.com...] into your browser. Even though that subdirectory does not exist, you should be redirected to your script with "page=something.html" passed to the script.

The flags T and L specify a mime-type for the script you are redirecting to, and specify that all mod_rewrite processing should end if the RewriteRule matches and is processed, respectively.

I'm not sure that the T part is really needed, but you could try testing it both ways.

For details on mod_rewrite, see the Apache mod_rewrite documentation linked from this Introduction to mod_rewrite [webmasterworld.com] post.

HTH,
Jim

9:33 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Birdman,

As long as "pages" and "hostinplans" are mutually-exclusive directories and no further rewrites need to take place after a match, I'd recommend leaving that [L] in place.

Jim

9:40 pm on Mar 19, 2003 (gmt 0)

10+ Year Member



I am still not sure how to handle the old page links.

My old pages are listed on google as:
myweb.com/web_hosting.shtml

How would I fix it, when clicked on, that it would go to my new web hosting page at myweb.com/hostingplan/web_hosting.htm

Thanks again,
Rose

9:42 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, thanks for clarifying that. So my .htaccess files may be inefficient. Could you have a look and tell me if I should have the [L] on every line?

RewriteEngine On
RewriteRule ^(.*)_section$ /section.php?section=$1
RewriteRule ^veil_edges/(.*)$ /veil_edges.php?edge=$1
RewriteRule ^(.*)showpres_(.*)\.php$ /presentations/pres.php?pres_id=$2
RewriteRule ^(.*)_section/(.*)_shop\.php$ /shop.php?shop=$2&section=$1
RewriteRule ^(.*)_section/show_(.*)\.php$ /show.php?show=$2&section=$1 [L]

9:54 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Birdman,

Yes, probably. Usually, the only time you don't want [L] is if you want the results of a given matched RewriteRule passed on to the RewriteRules that follow it for further rewriting.

Basically, without [L] the rewrite engine keeps going and tries to match all the remaining rules in your .htaccess/httpd.conf file, so yes, that can be very inefficient. Use [L] whenever you intend that the rewrite take place and the new URL be used to serve a file immediately, with no more rewriting needed. (Note also that the [F] and [G] flags never need an accompanying [L] since they immediately terminate rewriting.)

HTH,
Jim

9:58 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, that did help alot! One more question. Does this apply to RedirectMatch and RedirectPermanent?
10:25 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



RedirectMatch and RedirectPermanent are completely different beasts. They don't have the equivalent of the [L], [N], [C], or [S] flags as far as I know. By reading the description, I would say that only the first Redirect directive which matches will be processed; The new URL is then returned with an appropriate 301 or 302 header to the client (browser) which will then issue a new request usng that URL. It is possible that this second request might match a different Redirect directive, and that the process could be repeated as many times as desired. However, that would be horribly inefficient, since the client would be involved.

Jim

 

Featured Threads

Hot Threads This Week

Hot Threads This Month