Can someone prompt me in the right direction to use mod_rewrite?

↘锁芯ラ 提交于 2019-12-10 15:18:03

问题


I am a bit new to this but I am trying to learn. I know that in PHP we can have a URL that ends in ?id=something but it seems to me that some websites are doing it just forward slashes. Is this possible using PHP? I saw these questions that seem to say it is possible but I haven't cracked it yet.

Can someone prompt me in the right direction for mod_rewrite? I would like to change the following URL

http://www.example.com/all-products/?onsale=true&topic=health&item=adaaos45

into

http://www.example.com/all-products/items-on-sale/health/adaaos45

Can you point me into the right way?


回答1:


What you are looking for is Apache Url Rewriting.

I will break it down a bit for you to help you understand, it helps to know a bit of regex.

there are a lot of answers here that discuss the method, but to sum it all up, you need to do three things.

# Switch on URL Rewriting
# Choose the URL that you want to display  
# Point it to the true URL that gives the information.

Options FollowSymLinks
RewriteEngine On

RewriteRule ^all-products/items-on-sale/health/adaaos45/?$ all-products/?onsale=true&topic=health&item=adaaos45 [NC,L]

Now of course, if you would want to match any results for the variables, you need to match the word in regex, and remember it, and use it in the last part of the line. But this should get you started on understanding what is going on.

With this code in your .htaccess, browsing to

http://www.example.com/all-products/items-on-sale/health/adaaos45

will show you the content that displays on this page.

http://www.example.com/all-products/?onsale=true&topic=health&item=adaaos45



来源:https://stackoverflow.com/questions/20842764/can-someone-prompt-me-in-the-right-direction-to-use-mod-rewrite

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