1

I'm trying to remove a PHP Get query from my domain. For example, instead of showing example.com/?url=1234, I'd like it to rewrite to example.com/1234, hiding the query but not removing it. I know this is possible and have read many tutorials on how to do this, but my code just isn't working. Here's what I'm currently trying:

RewriteEngine On 
RewriteCond %{QUERY_STRING} url=  
RewriteRule (.*) http://example.com/$1? [R=301]

What this is doing is stripping the query entirely, instead of just removing the ?url= segment.

Ryan Brodie
  • 6,554
  • 8
  • 40
  • 57

3 Answers3

0

Check this - htaccess rewrite for query string

and Htaccess Querystring rewrite

Community
  • 1
  • 1
TigerTiger
  • 10,590
  • 15
  • 57
  • 72
0

You are thinking about it the wrong way round. Rewriting is not something that the client sees, but something that are exclusive to the server.

This means that you can make example.com/1234 work as though the client had used example.com?url=1234.

To achieve this you would use the following lines:

RewriteEngine On   
RewriteRule (.*) http://example.com/url=$1 [QSA]
LasseValentini
  • 256
  • 1
  • 3
0

You need to extract the relevant part of the query string in the RewriteCond line.

RewriteEngine On
RewriteCond %{QUERY_STRING} url=(.*)(&|$)
RewriteRule (.*) http://example.com/$1%1? [R=301]

The above will discard any other query string parameters that you give (see examples below). It will also keep the filename if given.

Examples:

http://example.com/?url=1234       ---->   http://example.com/1234
http://example.com/a/?url=1234     ---->   http://example.com/a/1234
http://example.com/?url=1234&a=b   ---->   http://example.com/1234
diolemo
  • 2,621
  • 2
  • 21
  • 28
  • I've tried this line for line yet I'm getting a 404 error? – Ryan Brodie Apr 12 '12 at 09:15
  • This is rewriting, first into a redirect loop adding a tonne of &'s to the URL, then to the successfully stripped down URL with throws a 404 error. – Ryan Brodie Apr 12 '12 at 09:17
  • Does the (example) folder `1234` exist? If not then it will of course throw a 404 error. Would you like the request to still be given to the index file (`index.php` for example)? If so then this is NOT what you asked. I suggest to ask another question for that. – diolemo Apr 12 '12 at 09:21
  • See [this](http://uoco.net/d/dev/rewrite/?url=1234) link for an example of the above code in action. [Here](http://uoco.net/d/dev/rewrite/.htaccess) is the corresponding htaccess file. Notice the first link is of the form ?url=1234 as requested and the destination is .../1234/. – diolemo Apr 12 '12 at 09:24
  • Update question - I wish to 'hide it' not 'remove it', I thought this was obvious from how I wrote the question, but obviously not, my mistake, sorry. – Ryan Brodie Apr 12 '12 at 10:13
  • [This](http://uoco.net/d/dev/rewrite/.htaccess?reload) should do what you want. It requires that the request be handled by `index.php` (or equivalent). If you need it to work with any file (e.g. `abc.php?url=1234` and `def.php?url=1234`) instead of just the index then it will need further work. Your question still is lacking some detail but you should now have enough to work with. – diolemo Apr 12 '12 at 12:21