简体   繁体   English

.htaccess友好的URL防止重定向

[英].htaccess Friendly URL prevent redirect

I have a website which doesn't use friendly URLs and I would like to change that. 我有一个不使用友好URL的网站,我想对此进行更改。 My links so far are like this http://www.example.com/?l=EN&m=o36ASkkEVi , where the l variable contains the viewing language and the m contains the viewing category (menu). 到目前为止,我的链接类似于http://www.example.com/?l=EN&m=o36ASkkEVi ,其中l变量包含查看语言, m变量包含查看类别(菜单)。 Because the id of a menu item is unique I didn't have problems so far, even if it was a submenu item. 因为菜单项的ID是唯一的,所以到目前为止,即使它是子菜单项,我也没有遇到任何问题。 But now that I am trying to change it to Friendly URLs I have the following problem: 但是,现在我试图将其更改为“友好的URL”,但是我遇到了以下问题:

Firstly, the friendly title of a menu item is not unique since (in my project at least) there can be multiple submenu items (categories) in different main menu items, with the same title. 首先,菜单项的友好标题不是唯一的,因为(至少在我的项目中)在不同的主菜单项中可以有多个具有相同标题的子菜单项(类别)。 For example, both menu items Camera and Mobile Phone both have a submenu called Instructions . 例如,菜单项CameraMobile Phone都有一个名为Instructions的子菜单。 So my goal is for the final friendly URL being something like http://www.example.com/EN/Mobile-Phone/Instructions and http://www.example.com/EN/Camera/Instructions . 因此,我的目标是使最终友好的URL像http://www.example.com/EN/Mobile-Phone/Instructionshttp://www.example.com/EN/Camera/Instructions

Secondly, the "depth" of the menu is not predefined. 其次,菜单的“深度”不是预定义的。 So there could be limitless subcategories of a category (menu). 因此,类别(菜单)可能存在无限的子类别。

In the past for small websites I was using static rewrites of htaccess (one rule for each redirection) but right now, where the articles and categories are uncountable, I cannot do the same. 过去,对于小型网站,我使用的是htaccess的静态重写(每次重定向一个规则),但是现在,文章和类别无法计数,我无法做到这一点。 I thought of rewriting the .htaccess file on every article or menu creation and editing to have a rule for each case, but I guess after some point that would make a huge .htaccess. 我想在每篇文章或菜单的创建和编辑过程中重写.htaccess文件,以针对每种情况制定规则,但是我想经过一番之后,这将产生巨大的.htaccess文件。 Is that solution legit at all? 该解决方案是否合法?

My question so is this. 我的问题是这样。 How can I make a rule at the .htaccess to always sent me to the index.php instead of trying to find a website's subfolder when I write http://www.example.com/EN/Camera/Instructions but with keeping the URL being 'visible' to PHP? 我如何在.htaccess处制定规则,以始终将我发送到index.php而不是在编写http://www.example.com/EN/Camera/Instructions时尝试查找网站的子文件夹,但要保留URL对PHP“可见”?

I know after that I can use the explode('/', $_SERVER['REQUEST_URI']) and achieve my goal by selecting from the database the correct language and article or menu, but I do not or cannot figure out a way on how to do the first part. 我知道之后我可以使用explode('/', $_SERVER['REQUEST_URI'])并通过从数据库中选择正确的语言和文章或菜单来实现我的目标,但是我不知道还是无法找出解决办法如何做第一部分。

Also, is there any way that .htaccess can get me all the items after the http://www.example.com/ in something like an array and pass it as variables to index.php ? 另外,.htaccess是否可以通过某种方式将http://www.example.com/之后的所有项目都以数组的形式获取,并将其作为变量传递给index.php

with this code in your .htaccess, it sends everything to index.php where $1 are your variables 在您的.htaccess中使用此代码,它将所有内容发送到index.php,其中$ 1是您的变量

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

this is also interesting: 这也很有趣:

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

whenever it finds a folder or a file, it wont direct it to index.php 每当找到文件夹或文件时,它都不会将其定向到index.php

I use the exact same system for my sites. 我为我的网站使用完全相同的系统。 Here is my .htaccess: 这是我的.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]

Here is my web.config (IIS) 这是我的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>

And i read the path from PHP like this, i ripped it from my Input class structure, but you get the basic idea: 我这样阅读了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;
  }

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

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