mod_rewrite的用于URL变量,除去空参数和使用多于1field用于第一和第二变量 [英] MOD_REWRITE for URL variables, removing empty parameters and using more than 1field for the first and second variables

查看:225
本文介绍了mod_rewrite的用于URL变量,除去空参数和使用多于1field用于第一和第二变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有得到的回答非常好,但需要修改一个previous问题,我无法回答这样创造了一个新的问题。

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.

在code我拥有的是:

The code I have is:

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]

本作品,但我需要它能够使一个,如果有或为其他的选择被用来做什么的变量和分辨出来。什么领域

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.

除了这一点,当我真正去,它存在的文件夹,它工作正常。举例来说,如果我去了一个名为测试文件夹,这是一个实际的文件夹:

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 

它输出到以下网址:

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.

请能有人告诉我,这可怎么办呢?我想AP preciate解决此问题的任何帮助。

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?

感谢你。

推荐答案

确定首先在多视图选项:根据Apache的手册:

OK first the MultiViews options: As per Apache manual:

Content negotiated "MultiViews" are allowed using mod_negotiation.

总之,如果你调用的文件 foo.html ,如果浏览器请求 http://example.com/foo 然后阿帕奇自动查找并添加扩展,并提供 foo.html 到浏览器而不是抛出的 404 。此行为导致许多重写规则失败,因此我关闭它在一开始在我回答你的previous的问题。

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.

现在排除真正的目录由重写规则正在处理,我们需要这样一个附加条件:<!code>的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值这样的:

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,但不提供像previous选项的灵活性。但是,这一次也很容易处理的重写规则。然而,对于这个选项重写规则将需要为每个组合的书面

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 并乐意prepare 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://本地主机/ N1 / V1 / N2 / V2 / N3 / V3 / N4 / V4 将在内部重定向到的http://本地主机/ 4 = V4和放大器; N3 = V3和放大器; N2 = v2和放大器; N1 = V1 对待每对由 / 名称 - 值对用于QUERY_STRING。 记住,如果URI没有连段数,例如:的http://本地主机/ N1 / V1 / N2 / 则将被重定向到的http://本地主机/ N1 = V1 ,丢弃多余的氮气

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.

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

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