4

I need to escape the "?" character in the url, so that it works with rewritten urls like search/why-is-it?-1.html

I currently have the following in .htaccess

RewriteEngine on
RewriteRule ^search/(.*)-([0-9]+).html$ index.php?search=$1&page=$2 [L]

Thanks

slimb
  • 53
  • 3

1 Answers1

6

Don't put a question mark in your URLs. The ? is reserved for the start of the query string.

To place it in a URL, encode it as %3F.

Read the RFC and this answer.

Update

If using PHP (and it looks like you are), you could do something like this (page requested is index.php/inter?net)...

<?php

var_dump($_GET);

$urlTokens = explode('/', $_SERVER['REQUEST_URI']);

$slug = end($urlTokens);

var_dump($slug);

Outputs

array(1) {
  ["net"]=>
  string(0) ""
}

string(9) "inter?net"

You can see $_GET is confused.

Community
  • 1
  • 1
alex
  • 479,566
  • 201
  • 878
  • 984
  • Yes, That's what I do, but it becomes inaccessible when linked from Google or Facebook, That's the problem (They decode the url which is normal). I've seen some sites (using mod_rewrite) with similar urls, which allows putting "?" in the url without affecting anything.. That's why I asked if there is anyway to escape it or encode it using regex (.htaccess / mod_rewrite) – slimb Mar 02 '11 at 19:14
  • @slimb Can you link to these URLs? – alex Mar 02 '11 at 23:11
  • Thanks for your reply, but the problem is the query doesn't get passed/sent to the php file so I can manipulate it.. It just, once I put a "?" in the url it returns a 404 error. – slimb Mar 03 '11 at 22:44
  • @slimb Did you use the code I used above? Why do you so badly want a `?` in your URL? Being against the RFC, it may break at any time. – alex Mar 03 '11 at 23:15