using MOD_REWRITE to change web URLs to point to PHP script

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
brianharrell
Forum Newbie
Posts: 5
Joined: Tue Mar 11, 2003 10:36 am
Location: MN USA

using MOD_REWRITE to change web URLs to point to PHP script

Post by brianharrell »

Thank you for your time!

I want URLs like this

http://www.alltimefavorites.com/pro/dog ... blue/large

to redirect to

http://www.alltimefavorites.com/pro/

where the webserver will look for index.php by default and will parse the data from the original URL to read from a MYSQL db.
(I have the parsing script already to break the url data into an array)

I know that if I use
http://www.alltimefavorites.com/index.p ... blue/large
it works fine but the index.php looks stupid in this case.

How do I use mod_rewrite to do this without sending any error or redirect messages back to the search engines or browsers?

Thank you very much for your time.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Some links to a tutorial on this subject were posted here not so long ago but I'm damned if I can remember what they were.

Try a search on this forum and php builder. If I can dig the links out I'll post again.
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post by enygma »

actually, in PHP, it lets you drop the .php on the script, and you just add something like this to the httpd.conf:

<Files index>
ForceType application/x-httpd-php
</Files>

then, when they call this:
http://www.alltimefavorites.com/index/d ... blue/large

you can do:

Code: Select all

<?php
function processURI() {
  $array = explode("/",$_SERVER['REQUEST_URI']);
  $num = count($array);
  $url_array = array();
		
  for($i=1;$i<$num;$i++){	         
    $url_array["arg".$i]=$array[$i];  
  }	
  return $url_array;
}
?>
and return the parts of the URL after the /index/ - in this case:
[0]=>dog
[1]=>collars
[2]=>blue
[3]=>large

or something....
brianharrell
Forum Newbie
Posts: 5
Joined: Tue Mar 11, 2003 10:36 am
Location: MN USA

Post by brianharrell »

Thank you for your help.

Where would I put my script?

alltimefavorites.com/index

and what would happen to my regular html files at
alltimefavorites.com/minnesota/index.html

I really appreciate your time!
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post by enygma »

this would only effect things that were like

http://www.foo.com/index/test/1

and not

http://www.foo.com/minnesota/index.html
brianharrell
Forum Newbie
Posts: 5
Joined: Tue Mar 11, 2003 10:36 am
Location: MN USA

Post by brianharrell »

Wow that works great!

Which is the bigger performance hit?
mod_rewrite or forcetype on the server with 100,000 page views per day?

ForceType example also found in this article:
http://www.devarticles.com/art/1/143/2

Thanks!
brianharrell
Forum Newbie
Posts: 5
Joined: Tue Mar 11, 2003 10:36 am
Location: MN USA

Another issue with URLS

Post by brianharrell »

I need to have the following URLS rewrite to one PHP script.
I'm converting static pages to a dynamic php script.

http://www.mysite.com/blue/widgets-large-0993.htm
http://www.mysite.com/anysub/any-other- ... ts-093.htm
http://www.mysite.com/red/big-widgets-small-9923.htm
http://www.mysite.com/red/big/lovely/gr ... s-9923.htm

Where ANY url containing "widgets" would forward to
http://www.mysite.com/myscript.php

I've tried many options:

RewriteEngine on
Options +FollowSymlinks
RewriteBase /
Rewriterule ^.*WIDGETS$ myscript.php?id=uri=%{REQUEST_URI} [L]

Does not work. File not found error
Post Reply