简体   繁体   English

多个不同的php页面需要友好的URL-重写URL

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

I have php website has multiple different php pages: 我的php网站有多个不同的php页面:

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: 我的htaccess文件如下:

<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. 我的htaccess重定向到单个索引页面中的问题,如何使htaccess重定向到具有参数的每个页面。

Have new rules to handle 2 parameters and one parameter separately: 有新规则分别处理2个参数和一个参数:

<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>

Using htaccess is not a good options as you have so many pages. 因为您有很多页面,所以使用htaccess并不是一个好的选择。

In a long run it's not maintainable. 从长远来看,它是不可维护的。 Please use any routing module (like http://fatfreeframework.com/routing-engine ) 请使用任何路由模块(例如http://fatfreeframework.com/routing-engine

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM