How to define friendly URL rules using .htaccess?

狂风中的少年 提交于 2019-12-13 09:13:54

问题


Long time I am trying to define Rewriting rules but I could not succeed yet? I have a simple page http://www.myURL.com/pdf.php?id=2. I want to make it friendly by this: http://www.myURL.com/pdf/2/ ...

I wrote this rule after spending time on google:

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^pdf/([0-9]+)\$ pdf.php?id=$1. 

When I uploaded my .htaccess file on server and try to run first it give me 500 ERROR. Second time it loaded page but it could not show me friendly URL; it was showing as before. Kindly tell me better solution, I followed many instructions to make it useful but could not successful. My Client are not going further unless I showed him friendly URL. kindly help me as soon as possible. This is my client hosting Organization: Justhost.com.


回答1:


You can do this using Apache's Rewrite Engine. Something like this:

RewriteEngine On
RewriteRule ^pdf/([0-9/]+)$ /pdf.php?id=$1 [L]

This will allow you to go to http://yoursite.com/pdf/192 instead of http://yoursite.com/pdf.php?id=192




回答2:


Here is a very simple tutorial on using Apache's mod_rewrite to create friendly URLs.




回答3:


Probably a bit too much to take just right now but you could look at some of the available MVC frameworks to help you solve this. I'm guessing that the host only offers PHP 5.2 so I'd take a look at the routing/controller solution from Zend Framework 1.12 to solve this as that can be implemented into existing code without too much trouble.



来源:https://stackoverflow.com/questions/15371824/how-to-define-friendly-url-rules-using-htaccess

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