slimb
slimb

Reputation: 53

How to escape "?" using regex in .htaccess for mod_rewrite

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

Upvotes: 4

Views: 1538

Answers (1)

alex
alex

Reputation: 490123

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.

Upvotes: 6

Related Questions