Mod_rewrite clarification question. Only for dynamic urls?

只愿长相守 提交于 2019-12-24 16:42:34

问题


I've been asked to develop a mod_rewrite to change

http://www.example.com/health-and-fitness-tips/999/

into

http://www.example.com/health-and-fitness-tips/how-do-I-lose-10kg-in-12-weeks/

where 999 is the id and How do I lose 10kg in 12 weeks is the title.

Is this even remotely possible or is mod_rewrite only for changing something like

http://localhost/siteengine/products.php?id=123

into

http://localhost/products/123

???


回答1:


i'm guessing the answer meder gave is the one you're looking for but technically you can create a static map file to redirect a set of title strings to ids and it doesn't have to execute an external prg executable:

RewriteMap static-title-to-id txt:/tmp/title_to_id.txt
RewriteRule ^/health-and-fitness-tips/(.*)/ /health-and-fitness-tips/${static-title-to-id:$1}/ [L]

with the contents of the /tmp/title_to_id.txt file something like this:

how-do-I-lose-10kg-in-12-weeks  999
some-other-title                988
and-another                     983



回答2:


That's not practically do-able AFAIK ( dynamic-wise, without extra modules ). The first step you need to do is convert all the links to SEO ones, then you need to feed the titles as a parameter to a page.

Example:

<a href=/health-and-fitness-tips/how-do-I-lose/>

Condition: ^/health-and-fitness-tips/(.*)$/
Rewrite to: ^/page.php?title=$1

Some of this is pseudo code, but parse $_GET['title'] in page.php and query the database based on the permalink.

You'll also need to setup 301s for each permalinked URI so search engines don't get lost after you've done this.




回答3:


You could do this, but it would be messy and laggy. You'd still need a script to perform the rewriting.

Check out the documentation for the RewriteMap directive, in particular the bit on the prg map type -- using an external program to provide URL mapping services.

Now, mind you, I think this is a bad, bad idea, but it would let you get this done.




回答4:


Mod_rewrite just applies regular expression to the URL so getting ID from Title or vai versus is not possible (as far as I know).



来源:https://stackoverflow.com/questions/1515639/mod-rewrite-clarification-question-only-for-dynamic-urls

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