Forum Moderators: phranque
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?
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
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
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]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]+)$ /index.php?album=$1 [L]
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 :(
>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.
^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]