Mati
Mati

Reputation: 781

PHP URL/slug accept chars

I need to write simple routing system, I have only one question.

When I have url/slug like this

/article/1/simple-article-1

What characters should be allowed there.

Of course letters, digits, '-', '/' and?

Upvotes: 0

Views: 333

Answers (2)

Federkun
Federkun

Reputation: 36924

What characters should be allowed there.

Usually slugs are all lowercase, with accented characters replaced by letters of the english alphabet and blank characters replaced by a - or an _. Punctuation marks like the period, comma, question mark, exclamation point, apostrophe and quotation mark are generally removed. It may be also truncated to keep a reasonable length.

The reserved chars that may have a particular meaning in the URI are: !, *, ', (, ), ;, :, @, &, =, +, $, /, ?, #, [ and ]. If the character would conflict with a reserved character's purpose, then the conflicting data must be percent-encoded before the URI is formed.

Once you product the URI from its component parts, if you want add characters that are not alpha, digit, -, ., _ or ~ you should always percent-encoding it.

Example:

/article/1/i!want!use!the!exclamation!mark <-- bad
/article/1/i%21want%21use%21the%21exclamation%21mark <-- good

Upvotes: 0

Cvetomir
Cvetomir

Reputation: 29

.htaccess:

Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1  [L,QSA]

PHP:

if(isset($_SERVER['QUERY_STRING'])) {
    if(!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $_SERVER['QUERY_STRING'])) {
        return false;
    }

    $info = explode('/', $_SERVER['QUERY_STRING']);

    ....
}

Upvotes: 1

Related Questions