Forum Moderators: phranque

Message Too Old, No Replies

rewrite rule with optional url parameters

Its driving me insane >_<

         

Ready

7:46 am on Aug 27, 2010 (gmt 0)

10+ Year Member



Alright ... first of all, I'm a real newb when it comes to regular expressions and writing up rewrite rules.
Redirecting sites with or without 1 or 2 parameter in the url (if they're always given and ot optional, that is...) is about as complex as it gets for me
But now I need to have a more complex rule for an url and I have absolutely no idea how to do this. Even after searching around the net for more then an hour I'm even more confused then before. :(

Anyways, this is how the url can look like:
showchapters.php?s=seriesname&start=20&order=title&asc=asc

Now my problem is, all these parameters are optional and aren't always given so it could also look liike

showchapters.php?s=seriesname&start=20&order=title
or
showchapters.php?s=seriesname&start=20
or
showchapters.php?start=20&order=title&asc=asc
or
showchapters.php?start=20
or
showchapters.php?order=title
or
showchapters.php?s=seriesname
or without any parameters at all
showchapters.php
and so on...

And the way I need it to be rewritten is
showchapters/seriesname/20/title/asc/
(whereby all those "subfolders" naturally need to be optional to the url)

Any help or example of how to do this would be appreciated

jdMorgan

12:45 pm on Aug 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What do you plan to do with the "static-looking friendly URL" in the case that a query string parameter is missing.

You need a plan to eliminate the possibility of having duplicate content appearing at more than one single "version" of the new friendly/static URL. This means that all of the parameters in the new friendly/static URL must always be populated, and they must always be in the same order...

You could of course simply insert "/null" or "/-" (or "/0" anything similar) in the new friendly/static URL, but it would be faster/easier if you tell us your preference up-front.

Also for each parameter, please specify the acceptable characters, and for numeric ranges, the acceptable values. While not strictly necessary, doing so often results in *much* more efficient regex patterns and code.

The solution is a bit complex, and answering these questions up-front may significantly simplify both the code and the discussion.

Jim

Ready

6:03 am on Aug 31, 2010 (gmt 0)

10+ Year Member



I think I managed to get it done by myself afer taking a break from it for the weekend.
I made it so that "seriesname" is always given and "start" is always given when I add sorting to the string (as in "/someseries/0/title") when asc/desc is not given its descending order by default. Its probably totally messy the way I did it but at least it works :)


#### showchapters.php
RewriteRule ^showchapter/([a-z0-9\-/]+)/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/$ showchapters.php?s=$1&start=$2&order=$3&asc=$4 [NC,L]
RewriteRule ^showchapter/([a-z0-9\-/]+)/([a-z0-9]+)/([a-z0-9]+)/$ showchapters.php?s=$1&start=$2&order=$3 [NC,L]
RewriteRule ^showchapter/([a-z0-9\-/]+)/([a-z0-9]+)/$ showchapters.php?s=$1&start=$2 [NC,L]
RewriteRule ^showchapter/([a-z0-9\-/]+)/$ showchapters.php?s=$1 [NC,L]
# without trailing slash
RewriteRule ^showchapter/([a-z0-9\-/]+)/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)$ showchapters.php?s=$1&start=$2&order=$3&asc=$4 [NC,L]
RewriteRule ^showchapter/([a-z0-9\-/]+)/([a-z0-9]+)/([a-z0-9]+)$ showchapters.php?s=$1&start=$2&order=$3 [NC,L]
RewriteRule ^showchapter/([a-z0-9\-/]+)/([a-z0-9]+)$ showchapters.php?s=$1&start=$2 [NC,L]
RewriteRule ^showchapter/([a-z0-9\-/]+)$ showchapters.php?s=$1 [NC,L]

jdMorgan

11:29 am on Aug 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you accept both trailing-slash and no-trailing slash URL-paths in your rules' patterns, then the "showchapters.php" script must check the original client request, and redirect requests in one form or the other to the single "correct" form. Otherwise, you are setting up a duplicate-content situation, where your page URLs are competing against themselves for search ranking. And the search engines --not you-- will be picking the "preferred" URL.

An easier solution might be to use only one of your four-rule-sets above, and add a preceding rule to redirect all non-canonical "showchapter" URL requests to the canonical URLs.

However, if your script can do this function, then you can get rid of one of the almost-duplicate rulesets by using the regular-expressions "one or zero" quantifier "?" on the trailing slash:

#### showchapters.php
RewriteRule ^showchapter/([a-z0-9\-/]+)/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/?$ showchapters.php?s=$1&start=$2&order=$3&asc=$4 [NC,L]
RewriteRule ^showchapter/([a-z0-9\-/]+)/([a-z0-9]+)/([a-z0-9]+)/?$ showchapters.php?s=$1&start=$2&order=$3 [NC,L]
RewriteRule ^showchapter/([a-z0-9\-/]+)/([a-z0-9]+)/?$ showchapters.php?s=$1&start=$2 [NC,L]
RewriteRule ^showchapter/([a-z0-9\-/]+)/?$ showchapters.php?s=$1 [NC,L]

I encourage you to take full advantage of regular expressions. In this case, adding one character to each of the four rules eliminates the need to create, maintain, and execute an additional four rules...

Jim

Ready

2:12 pm on Aug 31, 2010 (gmt 0)

10+ Year Member



Thanks for the tip. I changed my rules to the ones with the ? quantifier now and it works like a charm :)
Only thing left is to specify if expected parameters are alphanumeric or just numbers but that won't be hard to change at all. :)

jdMorgan

2:58 pm on Sep 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please note what I posted above. It is important to NOT rewrite both trailing-slash and no-trailing-slash URLs *unless* the script will redirect one of those variants to the other.

Failure to implement either a .htaccess or script canonicalization redirect essentially means that each of your pages is competing with itself, because it has two separate and distinct URLs by which it can be accessed. Search for the appropriately-titled thread here on WebmasterWorld -- "Duplicate Content - Get it right or perish." In essence *you* are causing each of your pages to have to compete against itelf for links and ranking.

You can depend on Google and other search engines to 'figure it out' (which they most often do), but sometimes, they make mistakes, so why depend on outsiders to 'get it right' when you can control it yourself?

Jim