mod_rewrite rules for clean URLs with optional parameters

僤鯓⒐⒋嵵緔 提交于 2020-01-03 04:01:07

问题


I have the current URL:

http://domain.com

When you visit this URL it by default loads:

http://domain.com/index.php

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]

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

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

EDIT: 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.


回答1:


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]

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/

Gives a 404 however

http://domain.com/val1

Works as expected. How come?

EDIT: 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]



回答2:


There is a trailing slash in some of the request examples that the regex in the rules are not matching.

You may try this:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteRule ^([^/]+)/?$ /index.php?var1=$1 [L,NC]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?var1=$1&var2=$2 [L,NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?var1=$1&var2=$2&var3=$3 [L,NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L,NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?var1=$1&var2=$2&var3=$3&var4=$4&var5=$5 [L,NC]


来源:https://stackoverflow.com/questions/15735590/mod-rewrite-rules-for-clean-urls-with-optional-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!