Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewite with Multiple Variables

but NOT all these variables are required

         

Gian04

8:37 am on Oct 13, 2008 (gmt 0)

10+ Year Member



I need to ModRewrite this

index.php?v=$1&s=$2&p=$3&c=$4

I know this one would do

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/page([^/\.]+)/([A-Za-z0-9-]+)/?.html$ index.php?v=$1&s=$2&p=$3&c=$4 [L]
(www.example.com/view/show/page1/category.html)

But by problem is the variables v, s, p and c are NOT REQUIRED to be always present. So using the mod rewite above will treat the following

www.example.com/show
as
www.example.com/index.php?v=$1
while it should be
www.example.com/index.php?s=$1

OR

www.example.com/page1/category.html
as
www.example.com/index.php?v=$1&s=$2
while it should be
www.example.com/index.php?p=$1&c=$2

How can I resolve this? Thanks

g1smd

9:18 am on Oct 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You will need multiple rules. The other rules will test the requested URL when only one or two parameters are present, and therefore you need a very clear naming structure for the URLs that people see in their browser, so that only one rule could possibly ever match any URL request made to your server.

What do "view" and "show" actually do? Are they static words? Are they redundant information? Beware that you must not have multiple URLs that map to the same content page, as that will cause many problems.

The URL request

www.example.com/page1/category.html
does not match your existing rule, because your rule requires there to be two elements, each of one character or more, and that are separated by slashes, before the word "page".

If the code had used

([A-Za-z0-9-][b]*[/b])
you would have the problem you describe, but luckily it uses
([A-Za-z0-9-][b]+[/b])
instead.

Some other questions:

Wouldn't it be usual to have the page number as the last item in the URL?

What page of content is returned if the page number is missing? Is it page one? If it is, then that is a Duplicate Content issue - two different URLs that both return the same content.

What URL is shown in the browser for page one when you go from the index to page one? What is the URL shown in the browser when you are on page one, having just gone from page two to page one? Are they different, even by one character? If so, that is another Duplicate Content issue.

Does the same content return at both non-www and at www URLs, or does one of them issue a 301 redirect?

[edited by: g1smd at 9:33 am (utc) on Oct. 13, 2008]

Gian04

9:32 am on Oct 13, 2008 (gmt 0)

10+ Year Member



What do "view" and "show" actually do? Are they static words?

They are variables
possible values
view = individual or group
show = title of a particular play
category = children / adult etc

Once the show variable is present, the other variable is absent. Things like that.

maxed

8:07 am on Oct 20, 2008 (gmt 0)

10+ Year Member



what you could do (i think) is maybe do something like:

www.example.com/view-nameofgroup/
RewriteRule ^view-([A-Za-z0-9-]+)/$ index.php?s=$1 [L]

www.example.com/view-nameofgroup/show-showname/
RewriteRule ^view-([A-Za-z0-9-]+)/show-([A-Za-z0-9-]+)$ index.php?v=$1&s=$2 [L]

where you add short words and a dash in front of some options and use modrewrite rules to distinguish what variables mean what using those words, thats how i was thinking of doing it for my site, but you would need alot of rules i guess...

Im not sure if there is any other way, maybe someway to search the uri for "show-" and fetch everything after that until a /, and then search the uri for "view-" until a slash, and then take the variables do a rewrite.. im not sure how/if that can be done though...

g1smd

8:29 am on Oct 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You might need a lot of rules. See this other thread where something fairly simple needed six rules.

[webmasterworld.com...]

I am working on something right now that has more than 40 rules, each for a specific format.