Jordan
Jordan

Reputation: 4502

Mod Rewrite syntax: path to GET

I'm having a bit of trouble with the mod_rewrite syntax for Apache. Here's what I need:

The path

"www.example.com/public/path/to/file.txt"

needs to become:

www.example.com/public/?p=path%2Fto%2Ffile.txt

That is, everything after "public/" should be URL encoded and added as GET parameter "p". Any simple code snippets to do this?

Upvotes: 1

Views: 32

Answers (1)

Jon Lin
Jon Lin

Reputation: 143896

Put these rules in the htaccess file in the public folder:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /?p=$1 [L,B]

The important thing here is the B flag which makes sure the /'s get encoded into %2F. Though, I'm not sure whether you really need it.

Upvotes: 1

Related Questions