0

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!

Hugh Brackett
  • 2,706
  • 14
  • 21
JCraine
  • 1,159
  • 2
  • 20
  • 38

2 Answers2

0

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.

RoUS
  • 1,888
  • 2
  • 14
  • 29
  • Unfortunately didn't work for me, however I did get some good results with the following: RewriteRule ^(\w+)/(\w+)/?$ /#/$1/$2/ [NE,R,L] – JCraine Feb 07 '11 at 23:41
  • @JCraine Yeah, I missed the `.htaccess` tag. I've modified the answer appropriately. – RoUS Feb 09 '11 at 15:44
  • So with friendly URLS, if you've specified a link; mysite/video/ to go to mysite.com/video?id=20 the search engine will pick up the first url right? – JCraine Feb 11 '11 at 23:39
0

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;
?>
Devtrix.net
  • 705
  • 7
  • 8
  • 1
    Wow, thanks! That's a great example. I did however stumble across a method which seems to work `RewriteRule ^(\w+)/(\w+)/?$ /#/$1/$2/ [NE,R,L] ` - it takes **mydomain.com/video/video1** to **mydomain/#/video/video1** I think the flags allow the hash(#) to pass through. Could this cause any SEO problems do you think? – JCraine Feb 07 '11 at 23:46
  • @JCraine The [NE] flag (no URI escaping of output) after the modrewrite rule actually solves the problem. So one doesn't need to bother with my previous solution, just add one flag :) Thanks! – Devtrix.net Sep 09 '11 at 09:04