Will
Will

Reputation: 1933

htaccess - rewrite URL to make querystring nice

I have a site with a folder, and a htaccess file within that folder. For the index.php file within that folder, I want to rewrite the querystring for a certain parameter, so that typing in this URL:

www.example.com/myfolder/myparameter

Behaves like this (ie makes $_GET['parameter'] = 'myparameter' in my code)

www.example.com/myfolder/index.php?parameter=myparameter

I have looked at many questions on StackOverflow, but have not managed to get this working. My code so far is

RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*) [NC]
RewriteRule ^$ %0 [QSA]

But that just isn't working at all.

Upvotes: 0

Views: 171

Answers (2)

foundry
foundry

Reputation: 31745

RewriteEngine on
RewriteRule (^.*/)([^/]+)$  $1index\.php?parameter=$2   [L,QSA]

update
sorry use @somasundaram's answer. Per-directory .htaccess rewrite rules lose the directory prefix:

When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution encounters the end of a rule set. See the RewriteBase directive for more information regarding what prefix will be added back to relative substitutions.

(from the apache docs)

Upvotes: 0

somasundaram
somasundaram

Reputation: 139

Please use this code

RewriteEngine on
RewriteRule (.*) index\.php?parameter=$1 [L,QSA]

Upvotes: 1

Related Questions