Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite - generic parm?

         

Karma

3:37 pm on Jul 22, 2008 (gmt 0)

10+ Year Member



Hi all,

Still having a few problems with my mod_rewrite, here is .htaccess:

RewriteEngine on
RewriteRule albumlist/([^/]+) /?albumlist=$1 [L]

mydomain.tld/albumlist/a is working fine, this display a list of albums that begin with A.

The links within this list are structured as:

mydomain.tld/abbasgreatesthits

I wish to rewrite this to:

mydomain.tld/?album=abbasgreatesthits

I was hoping to add a kind of generic parameter like this:

RewriteRule ([^/]+) /?album=$1 [L]

but this gives me a server error.

How do I best achieve such a rewrite?

jdMorgan

4:31 pm on Jul 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to figure out how your URLs will vary, and how to tell them apart, so you can decide, by looking only at the requested URL whether you want to rewrite it to /?albumlist=x or /?album=x

Once you have decided how to tell the URLs apart, then you implement that decision process in your code.

The first step is the hard part.

Being *vary* descriptive and specific about each and every part of the requested URL is required; Each URL-path-part and each name/value pair in the query string may be useful in doing this.

It also is critical to organaize and design your URLs to make this easy. If you don't you run into big problems. Looking at your new rule, you are going to get requests that get rewritten to /?album=albumlist, /?album=robots.txt, and /?album=favicon.ico because there is nothing in your URL structure or your code to stop this from happening.

The best solution is to use album URLs like /album/abbasgreatesthits

This is all about providing enough information for each RewriteRule to make a correct decision, based on the requested URL and its query string.

Jim

janharders

4:47 pm on Jul 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



maybe something like this could do the trick:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} /([^/]+)$
RewriteRule .* /index.php?album=%1

first line checks if the request isn't for an existing file (e.g. robots.txt etc)
second line checks if it's /foobar but not /foobar/ (weeding out directories, albumlist etc
and finally the redirect.
haven't tested it, just wrote it down, but I'm sure someone will correct errors ;)

Other than that, I agree with jdMorgan, it helps alot if you decide to use a clean structure like
/albumlist/a
/album/abbasgreatesthits
/artistlist/a
/artist/abba

jdMorgan

5:22 pm on Jul 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The primary advantage of the clean URL structure is precisely that it avoids the necessity of calling the OS to call the filesystem to go to disk and check if the URL resolves to an existing file. This file-exists checking becomes a big problem on busy sites -- particularly on shared servers, and can slow down a site to the point that it becomes unusable. So, the exists-check method should be avoided if you "plan for success" and expect your site to become popular...

If it *is* required, best performance will be achieved if the RewriteConds that check for file-exists are placed *last* in the code, and if the RewriteRule pattern is as specific as possible. For example:

First step, make exists-check the last RewriteCond:


RewriteCond %{REQUEST_URI} /([^/]+)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /index.php?album=%1 [L]

Next step, make RewriteRule pattern as specific as possible:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]+)$ /index.php?album=$1 [L]

Jim

Karma

5:42 pm on Jul 22, 2008 (gmt 0)

10+ Year Member



Other than that, I agree with jdMorgan, it helps alot if you decide to use a clean structure like
/albumlist/a
/album/abbasgreatesthits
/artistlist/a
/artist/abba

I'm obviously very confused or I'm again confusing without meaning too. With that in mind...

The clean URL folder structure janharders mentioned is exactly what I am using and I thought that's what I described in my OP.

Sorry guys :(

janharders

8:26 am on Jul 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ah, then you should be fine. you fooled us with the

>mydomain.tld/abbasgreatesthits
>I wish to rewrite this to:
>mydomain.tld/?album=abbasgreatesthits

I think ;)
You could use something like
RewriteRule ([^/]+)/([^/]+) /?$1=$2 [L]

but it might be better to create the folders you want to use (/albumlist/...) and put an .htaccess in there redirecting every request to /?albumlist=$1
RewriteRule ([^/]+) /?albumlist=$1 [L]
I think. Never got too into the whole RewriteBase-thing..
That way, requesting /foobar/ won't result in a call to your script with /?foobar=, but return a proper 404 as it should. could be done in your script of course, but why waste resources where they're not necessary.

jdMorgan

4:03 pm on Jul 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just make four rules, one for each pattern, starting with:
^albumlist/
^album/
^artistlist/
^artist/
and then add a regex pattern to handle the specific values that the following variable-value URL-path-part can take, and back-reference the result in the substitution URL-path, e.g.

^albumlist/([a-z])$ (match one lowercase letter for "list" value)
^album/(.+)$ (match one or more of any character for an album name)

Jim

[edited by: jdMorgan at 4:04 pm (utc) on July 23, 2008]