.htaccess 中的多个 mod 重写
我想要以下规则,但我似乎没有得到正确的设置。
末尾有或没有斜杠都应该转到:
,对于此后的每个变量 extra,我希望它的行为如下:
到
< ;domain>/?index.php?page=training-courses&val1=success&val2=another-value&val3=and-yet-another-value
如果无法选择无限前导变量,我想在页面变量之后至少有2个变量
这可能吗?我该如何解决这个问题?
到目前为止我有这个:
RewriteEngine On
RewriteRule ^test/([^/]*)/$ /test/index.php?pagina=$1&val1=$2
RewriteRule ^test/([^/]*)$ /test/index.php?pagina=$1&val1=$2
RewriteRule ^test/([^/]*)/([^/]*)/$ /test/index.php?pagina=$1&val1=$2
RewriteRule ^test/([^/]*)/([^/]*)$ /test/index.php?pagina=$1&val1=$2
RewriteRule ^test/([^/]*)/([^/]*)/([^/]*)/$ /test/index.php?pagina=$1&val1=$2&val2=$3
RewriteRule ^test/([^/]*)/([^/]*)/([^/]*)$ /test/index.php?pagina=$1&val1=$2&val2=$3
I want the following rules but I don't seem to get the right setup.
<domain>/training-courses/
both with or without the slash at the end it should go to:
<domain>/?index.php?page=training-courses
and for each variable extra after this I want it to behave like this:
<domain>/training-courses/success/another-value/and-yet-another/
to
<domain>/?index.php?page=training-courses&val1=success&val2=another-value&val3=and-yet-another-value
If it's not possible to have the option for unlimited leading variables, i'd like to have at least 2 variables after the page variable
Is this possible? and how do I get this sorted out?
I have this so far:
RewriteEngine On
RewriteRule ^test/([^/]*)/$ /test/index.php?pagina=$1&val1=$2
RewriteRule ^test/([^/]*)$ /test/index.php?pagina=$1&val1=$2
RewriteRule ^test/([^/]*)/([^/]*)/$ /test/index.php?pagina=$1&val1=$2
RewriteRule ^test/([^/]*)/([^/]*)$ /test/index.php?pagina=$1&val1=$2
RewriteRule ^test/([^/]*)/([^/]*)/([^/]*)/$ /test/index.php?pagina=$1&val1=$2&val2=$3
RewriteRule ^test/([^/]*)/([^/]*)/([^/]*)$ /test/index.php?pagina=$1&val1=$2&val2=$3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你说得很对,你不能按照你想要的方式将无限变量作为子文件夹处理,mod_rewrite 只是不支持它。
但是,为了回答您的第一个问题,要使尾随 / 可选,请将您的规则更改为以下内容:
You're quite correct, you can't handle unlimited variables as sub-folders in the way you would like to, mod_rewrite just doesn't support it.
However, in answer to your first problem, to make the trailing / optional, change your rules to the following:
经过几个小时的努力,它根本行不通。由于不支持无限变量,除了页面(pagina)之外,我可以使用两个变量。我现在所拥有的是这样的:
除了第一个重写规则从其他重写规则中排除文件夹之外,所有重写规则都不起作用。通过工作,我的意思是它实际上会将 URL 重定向到 index.php,但我无法通过即 $_GET['pagina'] 获取 PHP 中的值,
奇怪的是这会工作,尽管对于我的喜好来说太有限了:
After hours and hours of struggeling it just WON'T work. Since unlimited variables just isn't supported I'm ok with two variables besides the page (pagina). What I have now is this:
None of the RewriteRules work except from the first one which excludes folders from the other rewriterules. With working I mean it will actually redirect the URLs to index.php but I can't get the values in PHP through ie $_GET['pagina']
Strange thing is this WILL work, though is way too limited for my liking:
我想回到我自己的问题并回答它。一段时间以来,我正在使用我想要的东西。
首先,我的 htaccess 中有这个:
这允许随时访问真实文件。如果路径不存在,它将把整个路径传递给index.php。
在index.php中,我在顶部有以下代码:
给你一些可能性的例子:
domain.com/将包含home.php
domain.com/product/ 将包含product.php
domain.com/product/298833/ 将包含 Product.php 并将 298833 解析为 $val[1];
domain.com/product/298833/purple-candle 将包含 Product.php 并将 298833 解析为 $val[1];紫色蜡烛为 $val[2];请注意,不包括前导斜杠。不要紧。
总的来说,我愿意寻求改进或建议。现在,这绝对是我问这个问题时一直在寻找的答案。
I would like to come back to my own question and answer it. For a while now i'm using exactly what I want.
First off I have this in my htaccess:
This allows to access real files at all times. If the path does not exist it will pass the entire path to index.php.
In index.php I have the following code at the top:
To give you some examples of the posibilities:
domain.com/ will include home.php
domain.com/product/ will include product.php
domain.com/product/298833/ will include product.php and parse 298833 as $val[1];
domain.com/product/298833/purple-candle will include product.php and parse 298833 as $val[1]; and purple-candle as $val[2]; Notice the leading slash not being included. It does not matter.
I'm open for improvement or tips in general. For now this is definitely the answer I was looking for back when I asked this question.