更改 url 的模式以接受像 facebook 这样的用户名
Changing pattern of url to accept username like facebook
我需要从中隐藏用户个人资料 link
http://example.com/site/index?user_id=sami.yaqoub
像Facebook一样
http://example.com/sami.yaqoub
我将配置文件的规则更改为除外。
Config.php
<?php
..
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<user:[a-zA-Z0-9_-]\w+>'=>'site/index',// here is the code
),
),
...
?>
它适用于所有不包含任何点“.”的词。 , 所以我改成了这样
'<user:[a-zA-Z0-9_-.]\w+>'=>'site/index',// return error
但也没用。
在这种情况下,除此公式外的最佳方法是什么
Name.anything
提前致谢
尝试在根文件夹中创建一个 .htaccess 文件并将此代码粘贴到下面。
# if you have mod rewrite installed in your hosting space, you can enable pretty url for
# compressed css/js by uncommenting following lines
#RewriteEngine On
#Options FollowSymLinks
#RewriteRule ^packs/(\w+)\.(css|js) packs/jscsscomp.php?q=.
Options +FollowSymlinks
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L,QSA]
<Files .htaccess>
order allow,deny
deny from all
这也可能有帮助-> Yii how to get clean and pretty URL
我相信您想做的是创建一个 Slug
.
看看 here if you want to create it yourself or check any already made plugins 为您做的。
在你的正则表达式 [a-zA-Z0-9_-.]\w+
中你可以匹配 .helloworld
但不能匹配 hello.world
因为你只匹配第一个位置的点 .
字符。
你应该这样写:[a-zA-Z0-9_-.][\w.]+
.
我不确定,但也许 Facebook 不允许在第一个位置使用特殊字符,例如点或破折号.-
。在这种情况下,正确答案是:[a-zA-Z0-9][\w.]+
或更短的 \w[\w.]+
请注意,正则表达式中的 \w
匹配单词字符。 \w
等同于 [A-Za-z0-9_]
我需要从中隐藏用户个人资料 link http://example.com/site/index?user_id=sami.yaqoub
像Facebook一样 http://example.com/sami.yaqoub
我将配置文件的规则更改为除外。
Config.php
<?php
..
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<user:[a-zA-Z0-9_-]\w+>'=>'site/index',// here is the code
),
),
...
?>
它适用于所有不包含任何点“.”的词。 , 所以我改成了这样
'<user:[a-zA-Z0-9_-.]\w+>'=>'site/index',// return error
但也没用。 在这种情况下,除此公式外的最佳方法是什么 Name.anything
提前致谢
尝试在根文件夹中创建一个 .htaccess 文件并将此代码粘贴到下面。
# if you have mod rewrite installed in your hosting space, you can enable pretty url for
# compressed css/js by uncommenting following lines
#RewriteEngine On
#Options FollowSymLinks
#RewriteRule ^packs/(\w+)\.(css|js) packs/jscsscomp.php?q=.
Options +FollowSymlinks
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L,QSA]
<Files .htaccess>
order allow,deny
deny from all
这也可能有帮助-> Yii how to get clean and pretty URL
我相信您想做的是创建一个 Slug
.
看看 here if you want to create it yourself or check any already made plugins 为您做的。
在你的正则表达式 [a-zA-Z0-9_-.]\w+
中你可以匹配 .helloworld
但不能匹配 hello.world
因为你只匹配第一个位置的点 .
字符。
你应该这样写:[a-zA-Z0-9_-.][\w.]+
.
我不确定,但也许 Facebook 不允许在第一个位置使用特殊字符,例如点或破折号.-
。在这种情况下,正确答案是:[a-zA-Z0-9][\w.]+
或更短的 \w[\w.]+
请注意,正则表达式中的 \w
匹配单词字符。 \w
等同于 [A-Za-z0-9_]