-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UrlRule Problem with trailing slash (/) #7670
Comments
Yes, there's difference and it's intentional. |
@yiisoft/core-developers it's not the first time people are asking about it. Maybe we should redirect to proper URL instead of not allowing to use incorrect one altogether? |
is it somehow possible that something is added to the pattern so that a optional trailing / is in the pattern.
So that both url's would work. |
The documentation is wrong. It says "the leading and ending slashes are ignored": http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#url-rules |
It seems to a bigger problem than I've thought. Wordpress is adding |
While it could be solved serverside, it's better to do in Yii itself else there will be lots and lots of unexpected 404s. |
Could be done app-side like the following: $config = [
// ...
'params' => require(__DIR__ . '/params.php'),
// redirect to the page with the trailing slash
'on beforeRequest' => function () {
$app = Yii::$app;
$pathInfo = $app->request->pathInfo;
if (!empty($pathInfo) && substr($pathInfo, -1) !== '/') {
$app->response->redirect('/' . rtrim($pathInfo) . '/', 301);
}
},
]; |
This leads to problems with search engines, as URI with and without ending slash may be different documents. All site should use only one style and return 301 or 404 for wrong ones. |
301 is better. |
@samdark then it should be an option to choose between with/without ending slash. And option to allow both for people who don't care about seo. And maybe 404 as default option (for BC)? |
|
This redirect rule works for me in htaccess:
|
Add a route at last line and add a action
|
right, thanks gerpayt |
Not sure where all the slash/no slash ended up but ran into something as well when porting some Yii1 code over to Yii2. Docs have it stated incorrectly if the a requests getPathInfo() is used. In the docs it states -
When doing something like $request->getPathInfo(); it returns the trailing slash. This is not so much about handling the route, but different results expected for getPathInfo(). No big deal either way, just want the docs to match what it really is supposed to be, solved by rtrim() the slash prior to using the getPathInfo() value in my code, again not a huge one, just letting folks know the docs may not match functionality. Thanks Sandy |
@sauron918 please create a separated issue for your problem |
- resolve Url like in the original code - if request can not be resolved, try again with suffix (if any) - if request is then resolved, then redirect to the corresponding url To be able to do this repetition, a new function is created called 'resolveRequest'. This function looks like the old 'resolve' function. The old revolve function is use to act as a controller, including a redirect
slash should be inside regular expression: some improvements: 301 redirect, baseUrl, one redirect in case of many slashes
|
@samdark could you please take a look at my pull request? I hope it will help to solve the issue. I use this approach on sites I develop. |
Still waiting for write answer. Why I will use 301 redirect for trailing slash?. As you guys know in yii1 better for url manager. Some guys has post like that /projectname/projectspecifc_controller => projectspecifc_controller/index It's not perfect answer. I have configure like that My code working fine. Thanks |
Because search engines think |
Another .htaccess rewrite rule before checking directory.
and the config for return [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'suffix' => '/',
'rules' => [
'/' => 'site/index',
'product/<slug>' => 'product/view',
'product-category/<name>' => 'product-category/index',
'search/<keyword>' => 'search/result',
],
]; |
I've experienced similar issue with IIS. So if anyone still experiencing trailing slash issue, Please use following URL rewrite Rule in web.config
|
just use
|
Hi i have added a UrlManager rules like given below added to my config
If i then call a url like
But the Url with a trailing slash produces a 404 error on the SiteController.
This is because the UrlRule doesn't match.
if i change the UrlRule to the code below (\w* instead of \w+) the default controller action is called.
I also tested the UrlRules in yii1 but there the UrlRules from the top are also working with a trailing / in the Url but not in yii2.
So I guess there is some kind of "bug".
I Don't Know if the trailing slashes are removed in the Request in yii1 but not in yii2.
But there is clearly some difference between the 2 frameworks.
Regards Horizons
The text was updated successfully, but these errors were encountered: