用于 URL 变量的 MOD_REWRITE,删除空参数并为第一个和第二个变量使用超过 1 个字段 [英] MOD_REWRITE for URL variables, removing empty parameters and using more than 1field for the first and second variables

查看:16
本文介绍了用于 URL 变量的 MOD_REWRITE,删除空参数并为第一个和第二个变量使用超过 1 个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前有一个问题得到了很好的回答,但需要修改,我无法回答,所以创建了一个新问题.

I have a previous question that was answered very well but requires an amendment and I was unable to reply so have created a new question.

我需要一个 mod_rewrite 来实现以下内容:

I required a mod_rewrite that made the following:

www.example.com?page=var1&cat=var2&subcat=var3&subsubcat=var4

进入这个:

www.example.com/var1/var2/var3/var4/

但这也涵盖了 var2、var3 和 var4 的可选使用,因此如果这些变量为空,它仍然可以工作.

but that also covered the optional use of var2, var3 and var4 so if those variables were empty, it would still work.

这个问题的回答很好,但需要修改才能使用 var2、var3 和 var4 的不同字段选项.

The question was answered very well but it requires an amendment to work with different field options for var2, var3 and var4.

我的代码是:

RewriteEngine on
Options +FollowSymlinks -MultiViews

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/?$ /?page=$1 [L]

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/([^/]*)/?$ /?page=$1&cat=$2 [L]

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/([^/]*)/([^/]*)/?$ /?page=$1&cat=$2&subcat=$3 [L]

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/([^/]*)/([^/]*)/([^/]*)/?$ /?page=$1&cat=$2&subcat=$3&subsubcat=$4 [L]

这行得通,但我需要它能够做出一个 if-or-else 来选择哪个字段用于哪个变量并区分差异.

This works but I need it to be able to make an if-or-else for the choice of what field is being used for what variable and to tell the difference.

比如我们平时有下面两个字符串,很容易区分哪个字段对应哪个变量:

For example, if we have the following two strings normally, it is easy to distinguish what field goes for what variable:

www.example.com?page=var1&cat=var2&subcat=var3&subsubcat=var4

www.example.com?othervar1=var1&othervar2=var2&othervar3=var3&othervar4=var4

但是对于这个字符串,它需要能够区分变量是用于哪个字段的:

but for this string, it requires the ability to distinguish what field the variable is for:

www.example.com/var1/var2/var3/var4/

我需要 mod_rewrite 来选择使用哪个.

I need the mod_rewrite to choose which is being used.

除此之外,当我实际转到确实存在的文件夹时,它确实可以正常工作.例如,如果我转到名为test"的文件夹,这是一个实际文件夹:

As well as this, when I actually go to a folder that does exist, it does work correctly. For example, if I go to a folder called "test", which is an actual folder:

www.example.com/test/

它将它用作页面"字段的变量.如果我使用以下没有结尾的正斜杠:

it uses it as a variable for the "page" field. If I use the following without the end forward slash:

www.example.com/test 

它将以下内容输出到 URL 中:

it outputs the following into the URL:

www.example.com/test/?page=test

我需要它也适用于正常存在的文件夹.

I need it to work for folders that exist as normal also.

请有人告诉我如何做到这一点?如果您能帮助我解决此问题,我将不胜感激.

Please can someone tell me how this can be done? I would appreciate any help in resolving this issue.

另外,有人能告诉我一开始的-MultiViews"位是什么意思,因为我以前没有遇到过这个?

Also, can someone tell me what the "-MultiViews" bit means at the beginning as I have not come across this before?

谢谢.

推荐答案

首先确定 MultiViews 选项:根据 Apache 手册:

OK first the MultiViews options: As per Apache manual:

Content negotiated "MultiViews" are allowed using mod_negotiation.

简而言之,如果您有一个名为 foo.html 的文件,并且如果浏览器请求 http://example.com/foo,那么 Apache 会自动查找并附加扩展名并提供服务foo.html 到浏览器而不是抛出 404.这种行为会导致许多重写规则失败,因此我在回答您之前的问题时一开始就禁用了它.

In short if you have a file called foo.html and if browser requests http://example.com/foo then Apache automatically finds and appends extension and serves foo.html to browser rather than throwing 404. This behavior causes many Rewrite rules to fail hence I disabled it at the start in my answer to your previous question.

现在要排除重写规则处理的真实目录,我们需要一个额外的条件,如下所示:RewriteCond %{REQUEST_FILENAME} !-d

Now to exclude real directories from being handled by rewrite rules we'll need an additional condition like this: RewriteCond %{REQUEST_FILENAME} !-d

您在这里几乎没有选择.请告诉我你想要哪一个,我会提供相关规则.

You have few options here. Please tell me which one you would like to have and I will provide rules for that.

选项 1:在您的 URL 中同时包含 var namevar value,如下所示:

Option 1: Have both var name and var value in your URL like this:

www.example.com/page/var1/cat/var2/subcat/var3/subsubcat/var4
www.example.com/othervar1/var1/othervar2/var2/othervar3/var3/othervar4/var4

如果您愿意走这条路线,这条路线为您提供了最大的灵活性,并且在重写规则中很容易处理.对于此选项,只需编写一次重写规则,只要变量的数量不发生变化,您就可以进行未来的更改

This one gives you maximum flexibility and is very easy to handle in Rewrite rules if you are willing to go to this route. For this option Rewrite rules will need to be written only once and you will be fine for future changes as long # of variables don't change

选项 2:每个方案都有不同的开始.例如:

Option 2: Have a difference start for each scheme. For ex:

www.example.com/handler1/var1/var2/var3/var4
www.example.com/handler2/var1/var2/var3/var4

此选项为您提供更短的 URL,但不像之前的选项那样提供灵活性.但是这个在 Rewrite 规则中也很容易处理.但是,对于此选项,需要为每个组合编写重写规则

This one is gives you shorter URL but doesn't provide flexibility like previous option. But this one is also very easy to handle in Rewrite rules. However for this option Rewrite rules will need to be written for every combination

所以我会推荐选项 1,如果您也喜欢这个选项,我很乐意为您准备一个 .htaccess.

So I would recommend option 1 and would be happy to prepare a .htaccess for you if you like this option too.

Options +FollowSymlinks -MultiViews
RewriteEngine on

# 404 handler
ErrorDocument 404 /notFound.php

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^handler1/([^/]+)/?$ /?page=$1 [NC,QSA,L]
RewriteRule ^handler1/([^/]+)/([^/]+)/?$ /?page=$1&cat=$2 [NC,QSA,L]
RewriteRule ^handler1/([^/]+)/([^/]*)/([^/]*)/?$ /?page=$1&cat=$2&subcat=$3 [NC,QSA,L]
RewriteRule ^handler1/([^/]+)/([^/]*)/([^/]*)/([^/]*)/?$ /?page=$1&cat=$2&subcat=$3subsubcat=$4 [NC,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^handler2/([^/]+)/?$ /?othervar1=$1 [NC,QSA,L]
RewriteRule ^handler2/([^/]+)/([^/]+)/?$ /?othervar1=$1&othervar2=$2 [NC,QSA,L]
RewriteRule ^handler2/([^/]+)/([^/]*)/([^/]*)/?$ /?othervar1=$1&othervar2=$2&othervar3=$3 [NC,QSA,L]
RewriteRule ^handler2/([^/]+)/([^/]*)/([^/]*)/([^/]*)/?$ /?othervar1=$1&othervar2=$2&othervar3=$3othervar4=$4 [NC,QSA,L]


Options +FollowSymlinks -MultiViews
RewriteEngine on

# 404 handler
ErrorDocument 404 /notFound.php

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)(/.*)?$ $3?$1=$2 [N,QSA,DPI]
RewriteRule ^(/[^/]+|[^/]+/|/?)$ /index.php [L,QSA,DPI]

使用这些规则,http://localhost/n1/v1/n2/v2/n3/v3/n4/v4 的 URL 将被内部重定向到 http://localhost/?n4=v4&n3=v3&n2=v2&n1=v1 将每对由 / 分隔的 URL 段视为 name-value 对对于 QUERY_STRING.请记住,如果 URI 的段数不是偶数,例如:http://localhost/n1/v1/n2/ 那么它将被重定向到 http://localhost/?n1=v1,丢弃多余的 n2.

Using these rules a URL of http://localhost/n1/v1/n2/v2/n3/v3/n4/v4 will be INTERNALLY redirected to http://localhost/?n4=v4&n3=v3&n2=v2&n1=v1 treating each pair of URL segments separated by / as a name-value pair for QUERY_STRING. BUT keep in mind if URI doesn't have even number of segments eg: http://localhost/n1/v1/n2/ then it will be redirected to http://localhost/?n1=v1, discarding extra n2.

这篇关于用于 URL 变量的 MOD_REWRITE,删除空参数并为第一个和第二个变量使用超过 1 个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆