mod_rewrite URL to a deep link

1.8k Views Asked by At

I was wondering if this was possible in mod_rewrite, and if anyone knows the syntax for it.

Browser receives:

http://example.com/video/my-first-vid/

mod_rewrite redirects to:

http://example.com/#/video/my-first-vid/

Thanks!

2

There are 2 best solutions below

2
On BEST ANSWER

It seems you can't use mod_rewrite for redirecting to /#/video/my-first-vid/ as the # sign is urlencoded and it will redirect to http://example.com/%23/video/my-first-vid/ which obviously isn't what you're looking for. I tested this couple of days before on Apache 1.3.

You may create a separate script(s) for redirection. Add a new rules for this:

RewriteRule ^video/?$ /redirector.php?page=video [NC,L,QSA]    
RewriteRule ^video/(.*)$ /redirector.php?page=video&subpage=$1 [NC,L,QSA]

But you might also need to parse the query string manually as it could be smth like:

/video/my-first-vid/?refID=test

Here's some simple example on PHP:

<?php
  $page = (isset($_REQUEST["page"])) ? trim($_REQUEST["page"]) : "";
  $subPage = (isset($_REQUEST["subpage"])) ? trim($_REQUEST["subpage"]) : "";

  // setting the redirect URL, your conditions here
  if ($page == "video") 
  {
    $url = "/#/video/" . $subPage;
  } 
  else
  {
    $url = "/"; // some default url
  }

  header("Location: " . $url);
  exit;
?>
3
On

I'm not sure about embedding the '#' in the URI, since that's typically a fragment identifier, but you could try this:

RewriteRule "^(?<!#/)(.*)" "#/$1" [R=301,L]

That should redirect anything that doesn't have a '/#' in front of it to the same location with '/#' in front of it.