Interpret and direct with URL that after domain contains a hash

Asked

Viewed 252 times

4

I am creating a rule for interpreting addresses that after the domain begin with # followed by numbers or followed by letters whose rule should only be read if there is no file or directory matching the given path:

# 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’m trying to implement aims to direct the visitor to the file index.php at 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 receive a Slug:

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

    directs 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
    

    directs to:

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

Problem

The way it is now, nothing comes to PHP, IE, I do not have the variable mod nor the variable id or slug as appropriate.

  • As far as I know, the browser does not send the hash in the request.

  • 1

    @bfavaretto I have been conducting tests to ascertain what you said and it really seems that I cannot handle it the way intended.

  • 1

    @bfavaretto This is a site with 3 year single-page type (hundreds of sub-pages with navigation via script / CSS animations). A URL manipulation is now being applied, specifically the hash. The idea is to copy the URL, save it in the bookmarks, use it on social networks, etc and get through this manipulated URL to reach a specific content on the page.

2 answers

5


I don’t know if what you’re trying to do is possible.
One thing I’m sure of:

Browser does not transfer content from hash portions 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 is used location.hash.

3

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

To catch # by 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
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.