简体   繁体   English

如何替换破折号(-)而不是网址中的空格?

[英]How to replace dash(-) instead of space in url?

I am using Yii2, I am trying to change Url spaces to dash(-) .我正在使用 Yii2,我正在尝试将 Url空格更改为dash(-)

The browser I'm using puts plus(+) instead of spaces in Url, in default;默认情况下,我使用的浏览器在 Url 中放置加号(+)而不是空格

For example this sentence:例如这句话:

"how to replace dash instead of space" “如何替换破折号而不是空格”

changes to:更改为:

/mysite.com/how+to+replace+dash+instead+of+space /mysite.com/how+to+replace+dash+instead+of+space

But I want put dash(-) instead of plus(+) like blow Url:但我想把破折号(-)而不是加号(+)像吹Url:

/mysite.com/how-to-replace-dash-instead-of-space /mysite.com/how-to-replace-dash-instead-of-space

You can use the following rule in htaccess :您可以在 htaccess 中使用以下规则:

RewriteEngine on

#1If uri has spaces, convert them to hyphens and set an  env "hasspaces"
RewriteRule (.*)\s(.*) $1-$2 [N,E=hasspaces:yes]
#2if the env "hasspaces" is set, we will redirect spaces to hyphens 
RewriteCond %{ENV:hasspaces} yes
RewriteRule ^(.+)$ /$1 [L,R]

This will convert all spaces in uri to hyphens and redirect the url这会将 uri 中的所有空格转换为连字符并重定向 url

  • example.com/hello word example.com/hello word

to

  • example.com/hello-word example.com/hello-word

Before doing this Be sure because - (dash) is used in url routing convention by Yii2 and match with the upper case in action name在执行此操作之前请确保因为- (破折号)在 Yii2 的 url 路由约定中使用并与动作名称中的大写匹配

Controller Class Naming控制器类命名

Controller class names can be derived from controller IDs according to the following procedure:控制器类名称可以根据以下过程从控制器 ID 派生:

Turn the first letter in each word separated by hyphens into upper case.将每个单词中由连字符分隔的第一个字母变成大写。 Note that if the controller ID contains slashes, this rule only applies to the part after the last slash in the ID.请注意,如果控制器 ID 包含斜杠,则此规则仅适用于 ID 中最后一个斜杠之后的部分。 Remove hyphens and replace any forward slashes with backward slashes.删除连字符并用反斜杠替换所有正斜杠。 Append the suffix Controller.附加后缀控制器。 Prepend the controller namespace.前置控制器命名空间。 The following are some examples, assuming the controller namespace takes the default value app\\controllers:以下是一些示例,假设控制器命名空间采用默认值 app\\controllers:

article becomes app\\controllers\\ArticleController;文章变成了 app\\controllers\\ArticleController; post-comment becomes app\\controllers\\PostCommentController; post-comment 变成 app\\controllers\\PostCommentController; admin/post-comment becomes app\\controllers\\admin\\PostCommentController; admin/post-comment 变成 app\\controllers\\admin\\PostCommentController; adminPanels/post-comment becomes app\\controllers\\adminPanels\\PostCommentController. adminPanels/post-comment 变成 app\\controllers\\adminPanels\\PostCommentController。 Controller classes must be autoloadable.控制器类必须是可自动加载的。 For this reason, in the above examples, the article controller class should be saved in the file whose alias is @app/controllers/ArticleController.php;为此,在上面的例子中,文章控制器类应该保存在别名为@app/controllers/ArticleController.php的文件中; while the admin/post-comment controller should be in @app/controllers/admin/PostCommentController.php.而 admin/post-comment 控制器应该在@app/controllers/admin/PostCommentController.php 中。

for a complete guide see this http://www.yiiframework.com/doc-2.0/guide-structure-controllers.html and http://www.yiiframework.com/doc-2.0/guide-structure-controllers.html有关完整指南,请参阅此http://www.yiiframework.com/doc-2.0/guide-structure-controllers.htmlhttp://www.yiiframework.com/doc-2.0/guide-structure-controllers.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM