help with apache rewrite rule

This is a discussion on help with apache rewrite rule within the Apache Web Server forums, part of the Web Server and Related Forums category; I need to be able to give users URLs that look like this... http://www.domain.com/ABC123/page.php .......


Go Back   Usenet Forums > Web Server and Related Forums > Apache Web Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-11-2006
William Krick
 
Posts: n/a
Default help with apache rewrite rule

I need to be able to give users URLs that look like this...

http://www.domain.com/ABC123/page.php

....and have the apache rewrite engine change them to look like this...

http://www.domain.com/page.php?account=ABC123


I'm not really sure how to do this.


To further complicate matters, the users can also leave off the page
like this...

http://www.domain.com/ABC123/

....and should be redirected to...

http://www.domain.com/index.php?account=ABC123


Can anyone give me examples that show how to go about this?
Or at least point me at a resource that shows how to do these sort of
things?

Thanks in advance

  #2 (permalink)  
Old 05-11-2006
Jim Hayter
 
Posts: n/a
Default Re: help with apache rewrite rule

William Krick wrote:
> I need to be able to give users URLs that look like this...
>
> http://www.domain.com/ABC123/page.php
>
> ....and have the apache rewrite engine change them to look like this...
>
> http://www.domain.com/page.php?account=ABC123


RewriteRule ^/([^/]*)/page.php$ /page.php?account=$1

>
>
> I'm not really sure how to do this.
>
>
> To further complicate matters, the users can also leave off the page
> like this...
>
> http://www.domain.com/ABC123/
>
> ....and should be redirected to...
>
> http://www.domain.com/index.php?account=ABC123


RewriteRule ^/([^/]*)/ /index.php?account=$1

>
>
> Can anyone give me examples that show how to go about this?
> Or at least point me at a resource that shows how to do these sort of
> things?
>


Visit http://httpd.apache.org/ and read up on RewriteRule for your
version of Apache. Learn that regular expressions are fun.

> Thanks in advance
>


Good luck,
Jim
  #3 (permalink)  
Old 05-11-2006
William Krick
 
Posts: n/a
Default Re: help with apache rewrite rule

Hmmm. I think that I didn't make my original example clear enough.
"page.php" can be *anything*.php.

I need to capture everything between the last slash and the ".php" and
put that into a variable too, like the account number.

I know a little of regular expressions from using AWK back in college.

Let's see how much I remember. Disecting the example you posted...

RewriteRule ^/([^/]*)/page.php$ /page.php?account=$1

^ means the beginning of the line
/ followed by a slash
( ) followed by *something*, $1 that is
[^/] one or more of anything that is NOT a slash
/ followed by a slash
page.php$ followed by page.php at the end of the line.

So tell me if this is what I need to handle...

http://www.domain.com/ABC123/foo.php

....where foo can be anything:

RewriteRule ^/([^/]*)/([^/]*).php$ /$1.php?account=$2


Am I close?

  #4 (permalink)  
Old 05-12-2006
Ulrich Schöbel
 
Posts: n/a
Default Re: help with apache rewrite rule

Am Thu, 11 May 2006 13:30:15 -0700 schrieb William Krick:

> Hmmm. I think that I didn't make my original example clear enough.
> "page.php" can be *anything*.php.
>
> I need to capture everything between the last slash and the ".php" and
> put that into a variable too, like the account number.
>
> I know a little of regular expressions from using AWK back in college.
>
> Let's see how much I remember. Disecting the example you posted...
>
> RewriteRule ^/([^/]*)/page.php$ /page.php?account=$1
>
> ^ means the beginning of the line
> / followed by a slash
> ( ) followed by *something*, $1 that is
> [^/] one or more of anything that is NOT a slash

* = zero or more of anything ....

> / followed by a slash
> page.php$ followed by page.php at the end of the line.
>
> So tell me if this is what I need to handle...
>
> http://www.domain.com/ABC123/foo.php
>
> ...where foo can be anything:
>
> RewriteRule ^/([^/]*)/([^/]*).php$ /$1.php?account=$2
>
>
> Am I close?


Yes, you are:

RewriteRule ^/([^/]*)/([^/]*).php$ /$2.php?account=$1

will do it.

Kind regards

Ulrich


 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:16 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0