Marc
Marc

Reputation: 559

mod_rewrite rules for clean URLs with optional parameters

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.

Upvotes: 1

Views: 2082

Answers (2)

Marc
Marc

Reputation: 559

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]

Upvotes: 3

Felipe Alameda A
Felipe Alameda A

Reputation: 11809

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]

Upvotes: 1

Related Questions