3

How do I redirect all folders to my index.html and determine where users came from.

Firstly I have only index.html and .htaccess files.

Scenario:

  1. User click to link: mydomain.com/blabla (normally goes to 404)
  2. Web site redirect to user mydomain.com/index.html or mydomain.com/index.html?par=blabla
  3. in mydomain.com/index.html show alert "came from mydomain.com/blabla"

Is this possible? Thanks.

UPDATE**********

I found solve like this.

Firstly changed .htaccess file: https://stackoverflow.com/a/21663207/1173413

and use alert(window.location); as Polostor's said.

Community
  • 1
  • 1
qwerty
  • 105
  • 1
  • 9
  • Yes it is possible but have you tried anything yet ? – Caner Akdeniz May 01 '15 at 16:33
  • I wrote this in htaccess file `RewriteEngine on RewriteCond %{REQUEST_URI} !^/index.html$ RewriteRule .* /index.html [L,R=302]` and `alert(document.referrer)` in index.html file – qwerty May 01 '15 at 16:44
  • I was thinking you could have a common js function and on page load of every page, call this common function by passing the url of the current page, and do your redirect in that common function. And on redirect, pass the redirected page's url as a querystring and ready it in he index.html – Caner Akdeniz May 01 '15 at 16:47
  • Problem is I have no blabla folder and folder name can change by user. I editted question – qwerty May 01 '15 at 16:54
  • http://httpd.apache.org/docs/2.4/custom-error.html – radiaph May 01 '15 at 17:05

1 Answers1

1

I can imagine something like this.

File .htaccess to redirect the page and set the last page as getter. When .htaccess is in main folder it works for any subfolder.

RewriteEngine on
RewriteRule ^(.*) /index.html?page=$1 [R=301,L]

Edit: better solution should be https://stackoverflow.com/a/12500251/4745695

Rules are

  1. R stands for redirect with response code 301
  2. L means it is last rule

And in index.html it would be something like.

<html>
  <head>
    <title>My title</title>
    <script>
      alert(window.location.search.substring(1));
    </script>
  </head>
  <body>
  </body>
</html>

I took get method from How to get the value from the GET parameters? ( location.search in javascript respectively ) and .htaccess is well described on those pages http://www.jakpsatweb.cz/server/ (in Czech, to translate them use google translator).

Community
  • 1
  • 1
Polostor
  • 187
  • 6
  • 25
  • I tried your .htaccess with add "/" character: RewriteEngine on RewriteRule (.*) /index.html?page=$1 [R=301,L] as a result url is http://www.example.com/index.html?page=index.html There 's no "blabla" folder name – qwerty May 02 '15 at 09:40
  • And firefox say: "Routing Invalid" – qwerty May 02 '15 at 09:46
  • @qwerty Thanks, forgot two things - add ^ before (.*), that should do the magic with folders, and as you said the / before index.html - I changed it in my answer.. And for the "Routing invalid" I have never seen it and I can't even google much.. – Polostor May 03 '15 at 05:30
  • "Routing Invaild" is transleted by google from Turkish :) your old code goes to loop. `example.com/blabla` call `example.com/example.com/blabla` and its call `example.com/example.com/blabla/exmaple.co.....` – qwerty May 04 '15 at 12:23
  • Right, right, right.. I can see it now.. Sorry for that.. The best and cleanest possible solution is IMO http://stackoverflow.com/a/12500251/4745695 for the .htaccess – Polostor May 04 '15 at 16:29