You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Edwards Jim <je...@ur.se> on 2003/12/11 14:49:16 UTC

[users@httpd] mod_rewrite question

Hello,
I need to rewrite some URLs:
Case 1:
http://localhost/folder/parent.php -> http://localhost/parent

Case 2:
http://localhost/folder/child.php?parent=parent -> http://localhost/parent/child

is this possible using mod_rewrite on Apache.

Thanks
Jim

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] mod_rewrite question

Posted by Robert Andersson <ro...@profundis.nu>.
Robert Andersson wrote:
> Probably. [...] this would do it:

Probably not :( I must not be getting enough coffee...

>     RewriteEngine On
>     RewriteRule ^/folder/(\w+)\.php /$1/
>     RewriteRule ^/folder/(\w+)\.php\?parent=(\w+) /$2/$1

Correction:

    RewriteRule ^/folder/(\w+)\.php$ /$1/
    RewriteRule ^/folder/(\w+)\.php\?parent=(\w+)$ /$2/$1

Note the end anchor $; without it this wouldn't work very well.

Regards,
Robert Andersson

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] mod_rewrite question

Posted by Robert Andersson <ro...@profundis.nu>.
Edwards Jim wrote:
> I need to rewrite some URLs:
> Case 1:
> http://localhost/folder/parent.php
>     -> http://localhost/parent
>
> Case 2:
> http://localhost/folder/child.php?parent=parent
>     -> http://localhost/parent/child
>
> is this possible using mod_rewrite on Apache.

Probably. I am not certain of the semantics here, but assuming both "parent"
and "child" are variable names, this would do it:

    RewriteEngine On
    RewriteRule ^/folder/(\w+)\.php /$1/
    RewriteRule ^/folder/(\w+)\.php\?parent=(\w+) /$2/$1

I added a trailing slash to the first case, as I assumed it's was a
directory? You might need to change "(\w+)" to be a character class to
include whatever characters "parent" or "child" might contains, eg:
"([\w-_\.]+)", or maybe just "(.+)". If you're not very familiar with
regular expressions you should read:

    http://www.perldoc.com/perl5.6.1/pod/perlre.html

Regards,
Robert Andersson


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org