2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HumHubのURLのGETパラメータをキレイにする

Last updated at Posted at 2018-02-15

概要

HumHubでのpretty URLの設定方法。

関連

デフォルトでは

HumHubはデフォルトでは、以下のようなURL形式になる。

これはSEO的にも良くないので、以下のような形式にしたい。

Apacheの設定

.htaccessもしくはApacheのconfファイルに以下を追記する

.htaccessに書く場合

.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

confに書く場合

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule . /index.php

参考:httpd.conf と .htaccess でリライト設定が異なる

HumHubのconfigの設定

protected/config/common.phpのcomponentsの中に以下を追記する

'components' => [
    // ...
    'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Hide index.php
        'showScriptName' => false,
        // Use pretty URLs
        'enablePrettyUrl' => true,
        'rules' => [
        ],
    ],
    // ...
],

ProfileページのURLを修正

pretty urlを有効にすると、プロフィールページのURLにログインに使用するusernameが含まれるようになってしまう。(無効のときはguidが使用されている)
ログインIDがバレるのはセキュリティ的に良くないので、guidを使用する方式に変更する。

protected/humhub/modules/user/components/UrlRule.php

getUrlByUserGuid()メソッドは不要となるので削除します。

class UrlRule extends Object implements UrlRuleInterface
{
/* snip */

    public function createUrl($manager, $route, $params)
    {
        if (isset($params['uguid'])) {
-           $username = static::getUrlByUserGuid($params['uguid']);
-           if ($username !== null) {
+           $guid = $params['uguid'];
+           if ($guid !== null) {
                unset($params['uguid']);

                if ($this->defaultRoute == $route) {
                    $route = "";
                }

-               $url = "u/" . urlencode($username) . "/" . $route;
+               $url = "u/" . urlencode($guid) . "/" . $route;
                if (!empty($params) && ($query = http_build_query($params)) !== '') {
                    $url .= '?' . $query;
                }
                return $url;
            }
        }
        return false;
    }

    public function parseRequest($manager, $request)
    {
        $pathInfo = $request->getPathInfo();
        if (substr($pathInfo, 0, 2) == "u/") {
            $parts = explode('/', $pathInfo, 3);
            if (isset($parts[1])) {
-               $user = User::find()->where(['username' => $parts[1]])->one();
+               $user = User::find()->where(['guid' => $parts[1]])->one();
                if ($user !== null) {
                    if (!isset($parts[2]) || $parts[2] == "") {
                        $parts[2] = $this->defaultRoute;
                    }
                    $params = $request->get();
                    $params['uguid'] = $user->guid;

                    return [$parts[2], $params];
                }
            }
        }
        return false;
    }

-   /**
-    * Gets usernameby given guid
-    * 
-    * @param string $guid
-    * @return string|null the username
-    */
-   public static function getUrlByUserGuid($guid)
-   {
-       if (isset(static::$userUrlMap[$guid])) {
-           return static::$userUrlMap[$guid];
-       }
-
-       $user = User::findOne(['guid' => $guid]);
-       if ($user !== null) {
-           static::$userUrlMap[$user->guid] = $user->username;
-           return static::$userUrlMap[$user->guid];
-       }
-
-       return null;
-   }
-
}

参考

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?