.htaccess文件:动态重定向重定向、文件、动态、htaccess

2023-09-02 20:36:24 作者:月亮是我踹彎的

我有一个硬时间学习.htaccess文件重写规则,所以我希望有人能告诉我如何重写这样的:

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

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

这个:

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

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

这是一个动态的URL,并且可以通过分隔多个元素;最后。我希望我的用户能够以尽可能人类友好的URL类型。我希望有人能帮助!

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!

推荐答案

如果您有可变数量的参数,你不仅可以用htaccess的做到这一点。你可以重写在PHP:

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]

和解析 $ _ SERVER ['REQUEST_URI'] 在rewrite.php。

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

对于静态一批4个变量:

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

和使用 $ _ GET ['F'] = strtr函数的效率($ _ GET ['F'],'/',';'); 的index.php文件。

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