与可选参数的URL清洁mod_rewrite规则 [英] mod_rewrite rules for clean URLs with optional parameters

查看:240
本文介绍了与可选参数的URL清洁mod_rewrite规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的网址:

http://domain.com

在默认情况下,负载访问该网址是:

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

这意味着它们是可选的参数

Meaning they are optional parameters

我究竟做错了什么?我如何设置的.htaccess接受可选参数。

What am i doing wrong? How can I set up .htaccess to accept optional parameters.

编辑:我有许多问题在原来的问题,所以我只是使它成一个更容易understandtable问题。希望生病得到答案。

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但

Gives a 404 however

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]

这篇关于与可选参数的URL清洁mod_rewrite规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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