user3589620
user3589620

Reputation:

Redirect URL with GET variable to site with the GET variable in it

I would like to redirect, for example :

index.php?name=sarah 

to

names/sarah.php

The folder structure:

I've tried :

RewriteRule index.php?name=$1 /names/$1.php

and other ones, but didn't came to a solution.

Is this possible? If so, how can you do this?


Note:

The other way round is

RewriteRule ^names/([^/]*)\.php$ index.php?q=$1 [QSA,L]

which works fine.

Upvotes: 0

Views: 35

Answers (2)

Amit Verma
Amit Verma

Reputation: 41219

You first example wont work as you are trying to manipulate query strings using a RewriteRule but this is not allowed. You can try the following :

RewriteEngine on
RewriteCond %{THE_REQUEST} / index\.php\?q=([^\s]+) [NC]
RewriteRule ^ /names/%1.php? [L,R]
RewriteRule ^names/([^/]*)\.php$ index.php?q=$1 [QSA,L]

Upvotes: 1

Florian Lemaitre
Florian Lemaitre

Reputation: 5748

Here is your rule:

RewriteCond %{QUERY_STRING} name=([^&]*)
RewriteRule ^.*$ /names/%1.php?

Upvotes: 1

Related Questions