user3478635
user3478635

Reputation:

Query string in a mod_rewrite rule (.htaccess)

If I have a question mark in my q= Parameter, the .htaccess is redirect me to the 404 error-page.

This is my .htaccess Rewrite-Rule:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^search/(.*)/(.*)/(.*)$ results.php?q=$1&start=$2&type=$3 [L,QSA]

Does anybody know how I can allow a question mark in the get parameter ?

Upvotes: 0

Views: 227

Answers (1)

Giacomo1968
Giacomo1968

Reputation: 26066

Try using this instead. It will handle all all requests to search/. From three paths—as your example shows—to one path:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^/search/([a-z0-9]+)/?([^/]*)$ /results.php?q=$1&$2 [QSA]
RewriteRule ^/search/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/?$ /results.php?q=$1&start=$2&type=$3 [L]
RewriteRule ^/search/([a-z0-9]+)/([a-z0-9]+)/?$ /results.php?q=$1&start=$2 [L]
RewriteRule ^/search/([a-z0-9]+)/?$ /results.php?q=$1 [L]

Or try this without the / at the front of the URLs:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^search/([a-z0-9]+)/?([^/]*)$ results.php?q=$1&$2 [QSA]
RewriteRule ^search/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/?$ results.php?q=$1&start=$2&type=$3 [L]
RewriteRule ^search/([a-z0-9]+)/([a-z0-9]+)/?$ results.php?q=$1&start=$2 [L]
RewriteRule ^search/([a-z0-9]+)/?$ results.php?q=$1 [L]

This second one works on my local MAMP setup. If I create a results.php whose contents are this:

<?php

echo '<pre>';
print_r($_GET);
echo '</pre>';

?>

And then load the following URL; which I based off of your comment which states you got an error with /search/google/0/web:

 http://localhost:8888/search/google/0/web

This is result I get in my browser:

Array
(
    [q] => google
    [start] => 0
    [type] => web
)

Upvotes: 1

Related Questions