问题
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?
回答1:
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]
回答2:
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.
来源:https://stackoverflow.com/questions/20686284/allow-slash-in-url-htaccess