.htaccess file: Dynamic redirect

自闭症网瘾萝莉.ら 提交于 2019-12-24 07:38:36

问题


I've had a hard timing learning rewrite rules in .htaccess files, so I was hoping someone could show me how to rewrite this:

test.com/home/feed/toak/nyheter/vg

into this:

test.com/index.php?r=home&f=feed;toak;nyheter;vg

This is a dynamic URL, and can have multiple elements seperated by ; in the end. I want my users to be able to type in as human friendly URLs as possible. I'm hoping someone can help!


回答1:


If you have variable number of parameters, you can't do it only with .htaccess. You could rewrite in php:

RewriteEngine On
RewriteRule .* rewrite.php [NE,L]

And parse $_SERVER['REQUEST_URI'] in rewrite.php.

For static number of 4 variables:

RewriteEngine On
RewriteRule ^(\w+)/(\w+)/(\w+)/(\w+)/(\w+) index.php?r=$1&f=$2;$3;$4;$5

Or you could do (for any number of variables):

RewriteEngine On
RewriteRule ^(\w+)/(.*) index.php?r=$1&f=$2

And use $_GET['f'] = strtr($_GET['f'], '/', ';'); in index.php.



来源:https://stackoverflow.com/questions/3077451/htaccess-file-dynamic-redirect

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