user8707308
user8707308

Reputation: 1

HTACCESS redirection with parameters not working

I need to rewrite url on my PHP website but I'm not sure about the syntax.

I want to redirect :

'some_url?id=1' => 'new_url/1'

And when user type this :

'new_url/1'

I want him to be backend redirect to :

'some_url?id=1'

UPDATE: My last try:

RewriteRule /site/?id=$1    /site/$1

Upvotes: 0

Views: 93

Answers (2)

Croises
Croises

Reputation: 18671

You can use:

RewriteEngine on
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+some_url\?id=([^\s&]+) [NC]
RewriteRule ^ /new_url/%1? [R=301,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^new_url/(.+?)/?$ some_url?id=$1 [L,QSA,NC]

Upvotes: 2

Bananaapple
Bananaapple

Reputation: 3114

You can't match the query string in rewrite rules. You need to use a rewrite condition instead.

Stuff matched there with () can then be used in the rewrite rule with the %n notation (as opposed to $n.

So what you are looking for is something along these lines:

RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^some_url($|/) /new_url/%1?

Demo here:

http://htaccess.mwl.be?share=0e31a7bf-9711-533d-896b-461177c46ca2

Upvotes: 0

Related Questions