0

I have a webpage set up where the user can navigate to a certain item on the page like so:

www.example.com/models.php#DDD-34

At the moment it uses a # symbol to indicate the code of the item i want to link to. Would it be possible using .htaccess to allow a / symbol instead of the #

So instead of the above to link to a item its

www.example.com/models.php/DDD-34

Would this be possible in anyway? Because / means a path it brings sup a 404 error. what .htaccess code would allow this?

user3004208
  • 521
  • 2
  • 6
  • 10
  • Why in the world would you use an anchor instead of a get parameter? Why yould you keep "models.php" as part of a rewritable url? – Pierre Arlaud Dec 19 '13 at 16:09

2 Answers2

0

There's something you need to consider when you're putting together a solution like this. The # is a client side element, it never gets sent to the server as part of a request. This means, even if you want to be able to go to www.example.com/models.php/DDD-34 the only thing htaccess can do for you is externally redirect the browser back to www.example.com/models.php#DDD-34. That's it. The server doesn't know what to do with the #DDD-34 part since that's something that javascript running on your browser understands.

If you're fine with the browser's location bar changing back to www.example.com/models.php#DDD-34 then this is what you'd need:

RewriteEngine On
RewriteRule ^models\.php/(.*)$ /models.php#$1 [L,R,NE]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • If he takes it further and uses javascript to grab the hash combined with HTML5 pushstate to update the browser address bar, then he could do exactly what he wants with your .htaccess solution. I updated my answer to point that out. :-) – prograhammer Dec 19 '13 at 16:50
0

You can add something like this in your .htaccess file:

RewriteEngine On
RewriteRule ^models.php/(.*)$   /models.php?item=$1    [L,NC,QSA]

Then have a server side script (let's say PHP) fill in the javascript needed to navigate to that anchor:

<script> location.hash = "#" + <?=$_GET['item'];?>; </script>

Where <?=$_GET['item'];?> will add the item using PHP. (Note: if using PHP, you would want to further protect that piece of code with something like this: <?=addslashes($_GET['item']);?>

Alternatively, you could use Jon Lin's answer combined with using javascript to grab the hash item and HTML5's pushstate to achieve what you want without using a server side script.

Community
  • 1
  • 1
prograhammer
  • 20,132
  • 13
  • 91
  • 118