Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite .Grrrrrrr

Attempting a restructure

         

Karma

1:23 pm on Jul 17, 2008 (gmt 0)

10+ Year Member



Hi all,

Could really do with some help with a mod_rewrite. I'm aiming to re-structure my URLs in an existing site but I'm having problems getting things working, I'm looking at rewriting the following:

mydomain.tld/index.php ----------> mydomain.tld/
index.php?albuminfo=generic-album-title ---------> mydomain.tld/albums/generic-album-title/
index.php?albuminfo=another-album-title ---------> mydomain.tld/albums/another-album-title/
index.php?artistinfo=example-artist-name ---------> mydomain.tld/artist/example-artist-name/

Plus the more tricky part which is what I'm really having problems with:

mydomain.tld/index.php?songs=t ---------> mydomain.tld/songs/t/
mydomain.tld/index.php?song=thesongtitle&id=45678 ---------> mydomain.tld/thesongtitle/45678/

This is what I have for the top part, but I'm losing my images/style sheets :c/
---------------------
RewriteEngine on
RewriteRule ^([^_]+)/([^/]+)/?$ /index.php?$1=$2&$3=$4 [L]
---------------------

Anyone?

[edited by: Karma at 1:40 pm (utc) on July 17, 2008]

jdMorgan

2:26 pm on Jul 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We'll need examples of image and stylesheet URLs --examples of all possible variations-- in order to answer you question. You must come up with a regular-expressions pattern that differentiates an image/stylesheet URL-path from the URL-paths which you want to rewrite to your script. This can be based on the path, the filetype, presence of a file extension, or even whether or not the requested URL resolves to an actually-existing file. (This latter method is often used, but is the least efficient by far, execution-wise)

Be aware of two things: First, you may need to exclude images, stylesheets, external JavaScript files, and common files such as robots.txt, /w3c/p3p.xml, labels.rdf, etc. from being rewritten to index.php. Second, if you use page-relative linking to include these objects on your pages, you may need additional code to correct their URLs when requested, or you may want to use server-relative or canonical object links on your pages instead.

The solution is complicated by the fact that you have exposed (linked to) "/index.php" instead of just "/". This is going to cost you some extra coding/performance.

Jim

Karma

2:55 pm on Jul 17, 2008 (gmt 0)

10+ Year Member



Sorry, my bad. I pasted a 'chopped and changed' version as I was trying to get things working.

---------------------
RewriteEngine on
RewriteRule ^([^_]+)/([^/]+)/?$ /?$1=$2&$3=$4 [L]
---------------------

I link to my css and images using:
="/css/style.css"

[edited by: jdMorgan at 1:24 am (utc) on July 18, 2008]
[edit reason] Speling [/edit]

Karma

8:44 pm on Jul 20, 2008 (gmt 0)

10+ Year Member



Bump. Anyone? Really stuck here, have I provided enough information? :/

Karma

10:15 am on Jul 21, 2008 (gmt 0)

10+ Year Member



After more hacking around, I'm further to where I want to be. I managed to find the solution to these URLS:

index.php?albuminfo=generic-album-title ---------> mydomain.tld/albums/generic-album-title
index.php?albuminfo=another-album-title ---------> mydomain.tld/albums/another-album-title
index.php?artistinfo=example-artist-name ---------> mydomain.tld/artist/example-artist-name
mydomain.tld/index.php?songs=t ---------> mydomain.tld/songs/t

By using this:

RewriteEngine on
RewriteRule albums/([^/]+) /?albums=$1 [L]
RewriteRule artist/([^/]+) /?artist=$1 [L]
RewriteRule songs/([^/]+) /?songs=$1 [L]

However, I still can't get the tricky part working at all:

mydomain.tld/?song=thesongtitle&id=45678 ---------> mydomain.tld/thesongtitle/45678

I tried this, it's probably stupid and makes no sense:

RewriteRule ([^_]+)/([^_]+) /$1/id=$2 [L]

Karma

10:57 am on Jul 21, 2008 (gmt 0)

10+ Year Member



I think I fixed it!

RewriteRule ([^_]+)/([^_]+)/ /?song=$1&id=$2 [L]

jdMorgan

1:53 pm on Jul 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That does not seem to match any of your desired-rewrite descriptions. You'll get more/better assistance here by providing complete and consistent example URLs and the desired corresponding server filepaths, as requested in my last post above.

Jim

Karma

2:36 pm on Jul 21, 2008 (gmt 0)

10+ Year Member



Fair point, although if I confused/with-held information, it really wasn't intentional. I simply kept chopping away and tried things until it worked - I guess I find harder things in mod_rewrite confusing even though I've read many tutorials.

Anyway, thanks for the help provided earlier.

[edited by: Karma at 2:38 pm (utc) on July 21, 2008]