tags:

views:

41

answers:

2

I need a rewrite rule that will do an internal redirect from: <domain>/directory/<anything>/<anything>.php to: <domain>/directory/<anything>.php with <anything> passed as a parameter, and it needs to leave all other parameters alone.

I get headaches from mod_rewrite. My issue is <anything>.php, I cannot find any examples for anything like that.

Example: domain.com/directory/something/lol.php?param=value ought to internally redirect to domain.com/directory/lol.php?param=value&someotherparam=something

Thanks for the help! I have read through many tutorial sites, but I am in the dark here...

EDIT: Code tags added since it wasnt showing up properly >.>

Not all pages will have other parameters passed, some might have only one, some might have many. This is another bit that confuses me...

A: 
RewriteEngine On

RewriteBase /
RewriteRule ^rewrite/(.+)/lol.php$ rewrite/lol.php?someotherparam=$1 [QSA,R,L]
Ast Derek
It needs to be able to rewrite any page, not just lol.php
Cyclone
Wouldn't [R] force an external redirect in this case?
Lior Cohen
Ejem, wrong reading upheads, lost the 'internal' part
Ast Derek
+2  A: 
RewriteEngine On

RewriteBase /  
RewriteRule ^directory/([^/]+)/([^/]+)\.php$ directory/$2.php?someotherparam=$1 [QSA,L]

This will redirect domain.com/directory/something/lol.php?param=value to domain.com/directory/lol.php?param=value&someotherparam=something, as requested. (I'll note that currently, none of the other replies do so.)

Zarel
Deleted my answer. Zarel's regex is better. +1
Lior Cohen
Whoa. We have a winner, it works! +1, and accepted
Cyclone
Quick question, will this work in a subdomain?
Cyclone
Yes. [15 char min]
Zarel
Okay, so if I put it into the .htaccess of my subdomain it will continue to work? Do I need to change the base?
Cyclone
Aha, issue found! How can I make this work when accessing the page from /directory/lol without a page?
Cyclone
Well, what do you want `/directory/lol` to load? `/directory/lol.php`? `/directory/index.php?param=lol`?
Zarel