![](/img/trans.png)
[英]Rewrite Friendly php URL for multiple languages using .htaccess file
[英]Multiple different php pages need to be friendly URL - url rewrite
我的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
....
我需要这些网址如下所示:
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
....
注意:这些页面不只是我拥有的,我还有很多类似此页面的页面。
我的htaccess文件如下:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [NC,L]
</IfModule>
我的htaccess重定向到单个索引页面中的问题,如何使htaccess重定向到具有参数的每个页面。
有新规则分别处理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>
因为您有很多页面,所以使用htaccess并不是一个好的选择。
从长远来看,它是不可维护的。 请使用任何路由模块(例如http://fatfreeframework.com/routing-engine )
另一种解决方案是向规则添加一些唯一的路径,
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.