【问题标题】:.htaccess Friendly URL prevent redirect.htaccess 友好的 URL 防止重定向
【发布时间】:2013-05-23 09:40:57
【问题描述】:

我有一个不使用友好 URL 的网站,我想更改它。到目前为止,我的链接是这样的http://www.example.com/?l=EN&m=o36ASkkEVi,其中l 变量包含查看语言,m 包含查看类别(菜单)。因为菜单项的 id 是唯一的,所以到目前为止我没有遇到任何问题,即使它是子菜单项。但现在我试图将其更改为友好 URL,我遇到了以下问题:

首先,菜单项的友好标题不是唯一的,因为(至少在我的项目中)不同的主菜单项中可以有多个具有相同标题的子菜单项(类别)。例如,两个菜单项CameraMobile Phone 都有一个名为Instructions 的子菜单。所以我的目标是让最终的友好 URL 类似于 http://www.example.com/EN/Mobile-Phone/Instructionshttp://www.example.com/EN/Camera/Instructions

其次,菜单的“深度”没有预定义。因此,一个类别(菜单)可能有无限的子类别。

过去,对于小型网站,我使用 htaccess 的静态重写(每个重定向一个规则),但现在,文章和类别不可数,我不能这样做。我想在每篇文章或菜单创建和编辑时重写 .htaccess 文件,以便为每种情况制定规则,但我想在某些时候这会产生巨大的 .htaccess。该解决方案是否合法?

我的问题是这样的。如何在 .htaccess 中制定规则以始终将我发送到 index.php,而不是在我编写 http://www.example.com/EN/Camera/Instructions 时尝试查找网站的子文件夹但保持 URL 对 PHP 是“可见的”?

我知道之后我可以使用explode('/', $_SERVER['REQUEST_URI']) 并通过从数据库中选择正确的语言和文章或菜单来实现我的目标,但我不知道或无法弄清楚如何做第一部分。

另外,.htaccess 有没有什么方法可以让我得到http://www.example.com/ 之后的所有项目,比如数组,并将其作为变量传递给index.php

【问题讨论】:

    标签: php .htaccess friendly-url


    【解决方案1】:

    在您的 .htaccess 中使用此代码,它会将所有内容发送到 index.php,其中 $1 是您的变量

    RewriteRule ^(.*)$ index.php?path=$1 [L]
    

    这也很有趣:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    

    当它找到一个文件夹或文件时,它不会将它定向到 index.php

    【讨论】:

      【解决方案2】:

      我的网站使用完全相同的系统。这是我的 .htaccess:

       RewriteEngine on
       Options +FollowSymLinks
      
       # Redirect all valid requests to the engine or a valid file
       RewriteBase /
       RewriteRule !data/|javascript/|templates/|\.(js|ico|gif|jpg|png|css|swf)$ index.php [NC]
      

      这是我的 web.config (IIS)

      <?xml version="1.0" encoding="UTF-8"?>
      <configuration>
          <system.webServer>
              <rewrite>
                  <rules>
                      <rule name="Rewrite rules">
                          <match url="data/|javascript/|templates/|\.(js|ico|gif|jpg|png|css|swf)$" negate="true" />
                          <action type="Rewrite" url="index.php" />
                      </rule>
                  </rules>
              </rewrite>
          </system.webServer>
      </configuration>
      

      我像这样从 PHP 中读取路径,我从我的 Input 类结构中撕下它,但你明白了基本的想法:

        /**
         * Parse the current url string. This overrides the parent::parse();
         *
         */
        protected function parse( $stripExtensions = "html,htm,xml" )
        {
          $values = array();
          $regexp = '/(\.' . implode( '|\.', $stripExtensions ) . ')$/i';
      
          list( $url ) = explode( '?', $_SERVER['REQUEST_URI'] );
      
          // Strip trailing slash and allowed extensions
          if ( $url{strlen($url)-1} == '/' ) {
            $url = substr( $url, 0, strlen($url)-1);
            $this->extension = '/';
          } else {
            $matches = array();
            preg_match( $regexp, $url, $matches );
            if ( count( $matches ) > 1 ) $this->extension = $matches[1];
            $url = preg_replace( $regexp, '', $url );
          }
      
          $variables = explode( '/', $url );
          array_shift( $variables ); // First one is always empty
          $i = 0;
          foreach( $variables as $v ) {
            $values[$i++] = urldecode( $v );
          }
          return $values;
        }
      

      【讨论】:

        猜你喜欢
        • 2018-04-28
        • 1970-01-01
        • 1970-01-01
        • 2014-02-24
        • 1970-01-01
        • 2019-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多