JM4
JM4

Reputation: 6798

Please help me edit my .htaccess to allow hyphens in the urls

Update

In my original post I seem to have implied my variable name has a hyphen. My issue seems to be with the htaccess file below not matching on a hyphen but my $_GET variable content has a hyphen, not the variable itself.

original

I originally referenced http://php.net/manual/en/language.variables.variable.php which a user stated it is not possible to reference a php variable (by $_POST or $_GET) with a hyphen but this was written in 2009.

In one of my URL's, I allow users to enter a unique ID as "www.site.com/id123" which the "id123" is picked up as a $_GET variable with php. Some users have chosen to use "www.site.com/id-123" which does not work (no server error other than page cannot be found).

Any ideas on this?

.htaccess

RewriteEngine On

#if file or directory do not exist, try as an unique id
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z]+)$ index?ID=$1 [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]

Thinking through more, the rewrite rule above seems it may need modification to handle a hyphen request. Does this seem right?

Upvotes: 3

Views: 4756

Answers (7)

Álvaro González
Álvaro González

Reputation: 146540

In theory, you cannot:

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

In practice, well...

<?php

${'widh-hyphen'} = 'This variable has a pyphen';
${'Variable with funny name. Woot!'} = 'And this is even worse!';
var_dump(${'widh-hyphen'}, ${'Variable with funny name. Woot!'});

?>

... prints:

string(26) "This variable has a pyphen"
string(23) "And this is even worse!"

So I'd say you can.

Nothing of this should apply to GET and POST input data, though. Since Register Globals should always be disabled, your parameter become keys of the $_GET and $_POST associative arrays, and other rules apply:

A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08"). Floats in key are truncated to integer. The indexed and associative array types are the same type in PHP, which can both contain integer and string indices.

In such case, you must only take into account that dots are converted to underscores for historical reasons:

Dots and spaces in variable names are converted to underscores. For example becomes $_REQUEST["a_b"].

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385274

Your .htaccess file needs modifying to support hyphens.

Change

RewriteRule ^([0-9a-zA-Z]+)$ index?ID=$1 [QSA,NC,L]

to

RewriteRule ^([0-9a-zA-Z-]+)$ index?ID=$1 [QSA,NC,L]

Upvotes: 15

Udo G
Udo G

Reputation: 13121

Actually yes, variables can have whatever name you want. You only have to access them in another way.

This works:

$foo = "id-123"; 
$$foo = "hello";
var_dump($$ffoo);

You can check this by adding var_dump($GLOBALS); which will show a variable named "id-123".

You can not, however, access such a variable the normal way: var_dump($id-123); won't work.

However, since you are talking about GET/POST variables: These are only standard array keys and they also can have any name since you are accessing them through an array: $_GET["id-123"].

But note your example www.site.com/id123 implies a PATH_INFO and normally has nothing to do with $_GET or $_POST.

Upvotes: 0

mario
mario

Reputation: 145492

One could use the alternative syntax for quirky variable names:

${'my-var'} = 123;

But as the other answers said, that's wholly irrelevant for your case. You don't need it. Since you receive the data as value you wouldn't even need to honor the $_GET variable name transformation.

The problem in your case (page not found) sounds like a RewriteRule issue. Check that it does contain the hyphen in its regex:

RewriteRule  ^([\w+\-]+)$  get.php?id=$1

                #  ^^  missing

Upvotes: 0

Calum
Calum

Reputation: 5316

As Tom says, you're allowed hyphens in strings, so $_GET[user] = 'id-123' won't give you any problems, nor will sending any data like get.php?user=id-123.

I assume your .htaccess will rewrite www.site.com//id-123 to /getuser.php?user=id-123 If so, you shouldn't have problems.

But if you're trying, for some reason to create $id-123 then you will have some issues..

Upvotes: 0

Jacco
Jacco

Reputation: 23799

no, it is not possible

variable names start with a letter or underscore, optionally followed by a number, a letter or an underscore. http://www.php.net/manual/en/language.variables.basics.php

For an associative array key, you can use any string; since strings are allowed to include a hyphen, you could use $_GET['user-id'];.

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385274

A string's contents may contain a hyphen. A variable's name may not.

It's not clear in your question where the HTTP querystring comes into it. $_GET['some-variable'] is fine, as the array key is a string. $my-var is not fine.

If your page requests aren't working, it's far more likely to be a problem with your .htaccess file.

Upvotes: 5

Related Questions