Multiple different php pages need to be friendly URL - url rewrite

别来无恙 提交于 2019-12-13 04:44:51

问题


I have php website has multiple different php pages:

Example:
http://www.mywebsite.com/post.php?group_id=10&post_id=1
http://www.mywebsite.com/image.php?group_id=10&img_id=1
http://www.mywebsite.com/poll.php?group_id=10&poll_id=1
http://www.mywebsite.com/group.php?group_id=10
http://www.mywebsite.com/user.php?uid=158
....

I need these URLs to be like the following:

http://www.mywebsite.com/post/10/1
http://www.mywebsite.com/image/10/1
http://www.mywebsite.com/poll/10/1
http://www.mywebsite.com/group/10
http://www.mywebsite.com/user/158
....

Note: Not these pages just I have, I have many pages like this these page.

my htaccess file as the following:

<IfModule mod_rewrite.c>     
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [NC,L]
</IfModule>

the problem in my htaccess redirect to single index page, how can i make htaccess redirect to each page with its params.


回答1:


Have new rules to handle 2 parameters and one parameter separately:

<IfModule mod_rewrite.c>     
    RewriteEngine On

    RewriteRule ^(group)/(\d+)/?$ $1.php?group_id=$2 [NC,L,QSA]

    RewriteRule ^(user)/(\d+)/?$ $1.php?uid=$2 [NC,L,QSA]

    RewriteRule ^(image)/(\d+)/(\d+)/?$ $1.php?group_id=$2&img_id=$3 [NC,L,QSA]

    RewriteRule ^(poll|post)/(\d+)/(\d+)/?$ $1.php?group_id=$2&$1_id=$3 [NC,L,QSA]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [NC,L]
</IfModule>



回答2:


Using htaccess is not a good options as you have so many pages.

In a long run it's not maintainable. Please use any routing module (like http://fatfreeframework.com/routing-engine)




回答3:


another one solution by adding some unique paths to the rules,

example:

RewriteRule ^news/([^/]*)\.html$ /posts.php?id=$1 [L]
RewriteRule ^polls/([^/]*)\.html$ /polls.php?id=$1 [L]

result:

domain.com?posts.php?id=421 -> domain.com/news/421.html
domain.com?polls.php?id=17 -> domain.com/polls/17.html


来源:https://stackoverflow.com/questions/27170495/multiple-different-php-pages-need-to-be-friendly-url-url-rewrite

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