0

I have a website which doesn't use friendly URLs and I would like to change that. 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). Because the id of a menu item is unique I didn't have problems so far, even if it was a submenu item. But now that I am trying to change it to Friendly URLs I have the following problem:

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. 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.

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. 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. 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?

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.

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?

Dimitris Damilos
  • 2,363
  • 5
  • 23
  • 46

2 Answers2

1

with this code in your .htaccess, it sends everything to index.php where $1 are your variables

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

Petros Mastrantonas
  • 806
  • 1
  • 15
  • 41
1

I use the exact same system for my sites. Here is my .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)

<?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:

  /**
   * 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;
  }
Johan
  • 1,958
  • 11
  • 20