Static To Dynamic URL using Modrewrite

Hi,
I have site running and wish to convert the url
http://mysite.com/variable
to
http://mysite.com/index.php?action=variable

This requirement is opposite to the usual method to convert dynamic url to static url

How can this be achieved with mod rewrite rules

krishna,

I’m here to help members learn to create good mod_rewrite code (rather than offer free coding) so I’ll ask you first to read the tutorial linked in my signature then make a valid attempt. Post your code and I’ll be happy to critique it and offer additional pointers to get you where you want to go.

BTW, that’s basically what WordPress’s mod_rewrite does and they do a good job despite a few missteps that noobies might not be aware of. Look through this board for examples (and my critique).

Regards,

DK

Hi,
Below is what I have written

RewriteEngine On
RewriteRule ^index\.php\?data\=variable$ /variable [L]

And I wish to request the following from browser:
http://example.com/variable/

to get redirected to
http://example.com/index.php?data=variable

However this doesn’t redirect

Your rewrite rule is backwards (or so it seems)
Since you want to capture /variable/ and redirect it to index.php?data=variable, you want to do the following

RewriteEngine On
RewriteRule ^variable/? index.php?data=variable [L]

Then when you want to make it capture // and redirect to index.php?data=, you can do the following

RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/? index.php?data=$1 [L]

cp,

Good reply except that I’d prefer to check that the request is not for a directory (or extensionless file) before your RewriteRule. That’s important as not to do so could disable important functionality.

krishna,

If you read the tutorial, you’d have known that your attempt was “backward” as cp noted. It’s worthwhile to spend the time to read and understand the tutorial and look through the sample code (and explanations) at the end.

Regards,

DK

Bah! :headbang: I knew that! I can’t believe I forgot it. Next time I’ll nail it! :slight_smile:

cp,

:tup: Not bad anyway!

Regards,

DK