url rewriting index.php

2019-02-16 02:38发布

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

2条回答
够拽才男人
2楼-- · 2019-02-16 03:11

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.

查看更多
Ridiculous、
3楼-- · 2019-02-16 03:22

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

查看更多
登录 后发表回答