url rewriting an id with a string variable

巧了我就是萌 提交于 2019-12-08 05:10:04

问题


trying to figure out how to rewrite this url clientside

blog.com/post/how-to-get-the-ladies

to point serverside to

blog.com/post.php?id=123

i know how to do this:

blog.com/post/123

RewriteRule ^([^/]+)/?$ post.php?id=$1 [NC,L]

but how do you replace the id string with the post title slug?


回答1:


The webserver itself doesn't make this distinction and cannot translate from your "unique text identifier" to the database id. Therefore a .htaccess rule alone evaluated by the webserver will not help you. But how is it done on all those web-applications? Normally this translation is done by Joomla/Wordpress itself and it only works as long the "how_to_get_the_ladies" text is known and unique throughout the system/database.




回答2:


you can add rule that go to index file like :

RewriteRule ^(.*)$ index.php?url=$1

and in this file according to the title you can show the post that request




回答3:


I solved a similar problem recently. I would suggest looking into the RewriteMap directive and using an 'External Rewriting Program'.

There are some big limitations with RewriteRule in terms of maintainability and robustness. If you haven't gotten there yet you may eventually. Only simple rewriting rules can be written safely.

With a rewriteMap you can create a php or perl script, take advantage of your existing code base, and perform all the rewriting rules from a localized place in your code which easily sits in version control.

I believe you need access to the httpd.conf (or vhost) configuration file though, RewriteMaps (or some related directive) cannot be put in .htaccess files.



来源:https://stackoverflow.com/questions/992185/url-rewriting-an-id-with-a-string-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!