homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Using Mod_Rewrite for the first time
Dynamic URL Rewrite with .htacess

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

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:

End result should look like this:

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.



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

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)


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.



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


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.



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

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

My old pages are listed on google as:

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,


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

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)


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.)



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

Yes, that did help alot! One more question. Does this apply to RedirectMatch and RedirectPermanent?


 10:25 pm on Mar 19, 2003 (gmt 0)

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.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved