Reputation: 255
I'm trying to allow plus signs in the url, but I keep ending up at the 404 page. I'm trying to match urls like this: page/page+with+spaces
With "page+with+spaces" being the slug.
This is what I have in the config:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'page/<slug:[a-zA-Z\+\-]+>' => 'page/view',
],
],
Upvotes: 1
Views: 1327
Reputation: 2300
The plus sign is one of the ways to encode a space in the URL. By the time the URL is passed to the UrlManager
's rules, it has already been decoded.
So if you open http://example.com/page/page+with+spaces
in your browser, the string passed to the rule will actually look like this: /page/page with spaces
.
This may or may not be what you want. The same URL can be expressed as http://example.com/page/page%20with%20spaces
, and that is usually the case with modern browsers.
If you really want to serve up some page in response to http://example.com/page/page+with+spaces
, add space to the list of valid characters for slug like so: 'page/<slug:[a-zA-Z \-]+>'
and then set your slug to page with spaces
.
Update:
Apparently, +
is a valid encoding for space character only in the query component of URL and should be treated literally in the path component (where you are trying to use it). However, and I've tested this right now, pluses do get replaced with spaces in yii\web\Request
, so my suggestion will work.
My advice is to avoid pluses in URLs, even though technically they are valid.
Upvotes: 4