Toni Michel Caubet
Toni Michel Caubet

Reputation: 20163

Modrewrite is not sending $_GET params

I am trying to rewrite my urls:

RewriteEngine On
RewriteBase /

RewriteRule ^help/(.+)/(.+) help.php?helpid=$2
RewriteRule ^city/(.+)/(.+) city.php?cityid=$2

And even that the url contains the expected format: http://example.com/help/the-irelevant-title/5/

it seems that

isset($_GET['helpid'])) will allways fire false in help.php

Usually this system worked for me. Is there something I am missing here?

-EDIT-

As I can see that this should work (or at least one of you answers) I'm adding more code to the question:

Full .htaccess content

RewriteEngine On

ErrorDocument 500 /oohps.php
ErrorDocument 404 /oohps.php

RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

RewriteBase /

RewriteRule ^help/([^/]+)/([^/]+) help.php?helpid=$2 [QSA]
RewriteRule ^city/([^/]+)/([^/]+) city.php?cityid=$2 [QSA]

RewriteRule ^login$ login.php

Beggining of help.php

session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
$_GET['noseo'] = true;
include('htmlhead.php');
/* Leemos la id del grupo*/
$selected;
if(isset($_GET['helpid'])){
    $selected = $_GET['helpid'];
}else {
    echo '<script>alert("Modrewrite fails. Id is missing, id: '.$_GET['helpid'].'");location.href = "/"</script>';   /* Allways returns this*/
}

Upvotes: 0

Views: 136

Answers (4)

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

You may try this in the .htaccess file in root directory:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  ^/([^/]+)/[^/]+/([^/]+)/?  [NC]
RewriteRule .*              %1.php?%1id=%2             [L]

Redirects

http://domain.com/par1/anything/par2 with or without trailing slash

To

http://domain.com/par1.php?par1id=par2

Strings par1, anything and par2 are assumed to be dynamic and, according to the examples in the question, par1 is the name of the PHP script and also the first part of the key name in the redirected query.

Example when par1 = help:

Redirects:

http://domain.com/help/the-irelevant-title/5/

To:

http://domain.com/help.php?helpid=5

For permanent redirection, replace [L] with [R=301,L]


Although I did not test the other rules and don't know if they work as expected, the .htaccess file in the question should be modified like this to include the rule-set in this answer:

Options +FollowSymlinks -MultiViews
RewriteEngine On
ErrorDocument 500 /oohps.php
ErrorDocument 404 /oohps.php

RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  ^/([^/]+)/[^/]+/([^/]+)/?  [NC]
RewriteRule .*              %1.php?%1id=%2             [L]

# Next line is a suggested addition to prevent loops.
RewriteCond %{REQUEST_URI}  !login\.php [NC]
RewriteRule ^login$   login.php         [L]

Upvotes: 1

h2ooooooo
h2ooooooo

Reputation: 39532

Using [QSA] should work to forward the GET params. Also, changing .+ (greedy) to [^/]+ (meaning any character that isn't a slash repeated 1 or more times), it should fix your problem.

RewriteRule ^help/([^/]+)/([^/]+) help.php?helpid=$2 [QSA]
RewriteRule ^city/([^/]+)/([^/]+) city.php?cityid=$2 [QSA]

It can also be shortened to the following:

RewriteRule ^(help|city)/([^/]+)/([^/]+) $1.php?$1id=$3 [QSA]

From the apache manual:

When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

Consider the following rule:

RewriteRule /pages/(.+) /page.php?page=$1 [QSA]

With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] flag, that same request will be mapped to /page.php?page=123 - that is, the existing query string will be discarded.

Upvotes: 2

skrilled
skrilled

Reputation: 5371

is there a reason you have two variables? this works though:

RewriteEngine On
RewriteBase /    
RewriteRule ^help/([^/\.]+)/([^/\.]+)/?$ help.php?helpid=$2 [L]
RewriteRule ^city/([^/\.]+)/([^/\.]+)/?$ city.php?cityid=$2 [L]

Upvotes: 1

Wing Lian
Wing Lian

Reputation: 2418

The regex is too greedy with the frist .+ matching the slashes too causing the remainder of the regex to fail. The [^/]+ regex will match anything but the slash.

RewriteRule ^help/([^/]+)/([^/]+) help.php?helpid=$2
RewriteRule ^city/([^/]+)/([^/]+) city.php?cityid=$2

Upvotes: 3

Related Questions