url rewriting index.php

≯℡__Kan透↙ 提交于 2019-12-30 07:48:08

问题


i have urls like

http://mysite.com/index.php?p=resources
http://mysite.com/index.php?p=resources&s=view&id=938

but i want urls like

http://mysite.com/resources
http://mysite.com/resources/view/938

instead of making hundreds of rewrite rules i wonder if it would be possible to just have one? Ive head this is possible by "getting the uri and splitting it into parts" and then just add a rewrite rule for index.php

but how? could someone give an example or link a tutorial


回答1:


I happen to use this in .htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l 

RewriteRule .* index.php [L]

This essentially calls index.php no matter what is requested. Then inside your PHP code you can look at $_SERVER['REQUEST_URI'] to get the URL and parse it accordingly.

The RewriteCond lines are there to exclude direct calls to files. In my case I don't want stuff like requests for js/css/image files to go through index.php for performance reasons.




回答2:


Create a .htaccess file. Not somefilename.htaccess, it is simply named .htaccess.
Note: Your index.php and .htaccess file should be in the same directory.

Now try this on your .htaccess file

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9\-]+)$ index.php?p=resources
RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)$ index.php?p=resources&s=view&id=938
</IfModule>

see more about url rewrite here



来源:https://stackoverflow.com/questions/2652701/url-rewriting-index-php

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