带有可选参数的干净 URL 的 mod_rewrite 规则
我有当前的网址:
http://domain.com
当您访问此 URL 时,它默认加载:
When you visit this URL it by default loads:
http://domain.com/index.php
在我的 .htaccess 中,我有:
In my .htaccess I have:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?var1=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ /index.php?var1=$1&var2=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?var1=$1&var2=$2&var3=$3 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /index.php?var1=$1&var2=$2&var3=$3&var4=$4&var5=$5 [L]
.htaccess 为所有页面提供 404,所以我确定它是错误的.
The .htaccess gives 404 for all pages so Im sure its wrong.
我希望能够使用以下所有网址
I want to be able to have all of the following URLS work
http://domian.com/val1/
http://domian.com/val1/val2/
http://domian.com/val1/val2/val3
http://domian.com/val1/val2/val3/val4/
http://domian.com/val1/val2/val3/val4/val5
表示它们是可选参数
我做错了什么?如何设置 .htaccess 以接受可选参数.
What am i doing wrong? How can I set up .htaccess to accept optional parameters.
我在原始问题中有很多问题,所以我只是把它变成了一个更容易理解的问题.希望能得到答复.
I had to many questions in the original question so I just made it into one more easily understandtable question. Hopefully Ill get an answer.
好吧,我在反复试验后找到了答案:
Well I found the answer after quite a bit of trial and error:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)$ index.php?word=$1&media=$2&date=$3&sortby=$4&source=$5 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)$ index.php?word=$1&media=$2&date=$3&sortby=$4 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)$ index.php?word=$1&media=$2&date=$3 [L]
RewriteRule ^([\w-]+)/([\w-]+)$ index.php?word=$1&media=$2 [L]
RewriteRule ^([\w-]+)$ index.php?word=$1 [L]
这让我得到了我想要的结果.但是,如果最后有斜线,我会得到 404例子:
This gets me the results I was looking for. However if there is a slash at the end I get a 404 example:
http://domain.com/val1/
然而给出 404
http://domain.com/val1
按预期工作.怎么来的?
Works as expected. How come?
faa 尾随斜杠解决方案有效.最终规则如下所示:
faa solution for trailing slashes worked. Final rules look like this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?var1=$1&var2=$2&var3=$3&var4=$4&var5=$5 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?var1=$1&var2=$2&var3=$3 [L]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?var1=$1&var2=$2 [L]
RewriteRule ^([\w-]+)/?$ index.php?var1=$1 [L]