Interpret and direct with URL that after domain contains a hash

4

I am creating a rule to interpret addresses that after domain begin with # followed by numbers or followed by letters whose rule should only be read if there is no file or directory that matches the path indicated:

# Rewrite the url
<IfModule mod_rewrite.c>

    RewriteEngine On
    #DirectorySlash Off
    RewriteBase /

    # se não for ficheiro
    RewriteCond %{REQUEST_FILENAME} !-f

    # se não for directoria
    RewriteCond %{REQUEST_FILENAME} !-d

    # regra se começar por # seguido de letras com -
    RewriteRule ^#([a-zA-Z])/ index.php?mod=books&slug=$1 [L,PT]

    # regra se começar por # seguindo de números
    RewriteRule ^#([0-9]+)/ index.php?mod=books&id=$1 [L,PT]

</IfModule>

The idea I am trying to implement is to direct the visitor to the index.php file in the root of the domain if it is trying to access specific content that can be identified by its ID or a Slug of its name :

Objective

Below are two examples of what I'm trying to achieve:

  • If you get a Slug:

    http://www.example.com/#as-causas-e-os-acasos
    

    route to:

    http://www.example.com/index.php?mod=books&amp;slug=#as-causas-e-os-acasos
    
  • If you receive an ID:

    http://www.example.com/#23
    

    route to:

    http://www.example.com/index.php?mod=books&amp;id=#23
    

Problem

As it stands right now, nothing comes to PHP, that is, I do not have the variable mod nor the variable id or slug as the case may be.

    
asked by anonymous 13.03.2014 / 12:37

2 answers

5

I do not know if what you're trying to do is possible. Of one thing I'm sure:

The browser does not transfer hash portions content to the server during requests.

I think you will have to process the initial request directly from the browser using JavaScript. To get the hash value using Javascript, use location.hash .

    
13.03.2014 / 16:07
3

As @Evandro Araújo said, the server will never receive any content that comes after the # as it is only interpreted by the browser. So you can not handle these terms with Apache, you will have to use JavaScript.

To get the # from JavaScript you can use the following function:

function getHash() {
    var hash = window.location.hash; // pega o que tiver depois do #
    return hash.substring(1); // remove o primeiro caractere que é o # e retorna o restante da string
}
    
13.03.2014 / 16:46