jerome
jerome

Reputation: 91

mod_rewrite remove ?item=

I would like to remove "?item=" from an URL.

So, from

domain.com/?item=2

To

domain.com/2

How to simply remove question mark, key and equal sign?

Upvotes: 2

Views: 79

Answers (3)

antoni
antoni

Reputation: 5546

Usually in your programming language you would write the final url you would like to see in the url bar, so domain.com/2, and then in your .htaccess file you would make this unrecognized address to become recognized by your program with transforming it to domain.com/?item=2.

This redirection would be done like: RewriteRule ^\d+$ index.php?item=$1 [L]

[EDIT]

I know it's not fair if I edit my rule above - now I realize it's incomplete haha - so I change it here:

RewriteRule ^(\d+)/?$ index.php?item=$1 [L]

The only thing I want to add to accepted answer is that you can replace [0-9]+ with \d+.

Upvotes: 0

Amit Verma
Amit Verma

Reputation: 41219

To convert

  • /?item=2

to

  • /2

You can use this rule :

RewriteEngine on
#1) externally redirect the request "/?item=numbers" to "/numbers"
RewriteCond %{THE_REQUEST} /\?item=([^\s]+) [NC]
RewriteRule ^ /%1? [L,R]
#2) internally rewrite "/numbers" back to "/?item=numbers"
RewriteRule ^([0-9]+)/?$ /?item=$1 [L]

Upvotes: 3

markwalker_
markwalker_

Reputation: 12859

You can do that kind of rewrite like;

[apache]
RewriteEngine On

RewriteRule ^/([0-9]{1})[/]?$ index.php?item=$1

Upvotes: 1

Related Questions