0

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?

2
  • 'a-z 0-9~%.:_()@\-' change as per need Commented Jan 24, 2016 at 10:08
  • Have a look at this slug generator function cubiq.org/the-perfect-php-clean-url-generator and keep the international characters in mind. If you know how the slugified URL is generated, you can adapt your redirect rule. Commented Jan 24, 2016 at 10:49

2 Answers 2

1

.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']);

    ....
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.