PDA

Click to See Complete Forum and Search --> : [RESOLVED] Pretty URL's


TheBigB
Feb 23rd, 2010, 02:42 AM
Hi,

I'm giving pretty URL's a shot, but it's giving me headaches already.

The setup is .htaccess with the following content:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^\.]+)$ index.php?file=$1 [NC,L]
And in PHP retrieving $_GET['file'].

It kinda works, but the problem I'm facing is that when the path is deeper than 0 (e.g. /foo/bar) images and CSS don't work because the paths are incorrect (like img/foo.png).
Now rather than hardcoding in the paths to the images and css, I was wondering if there is a way of tweaking the .htaccess a little bit more to fix that.
Any other solutions would also be appreciated.

Thanks :wave:

I_Love_My_Vans
Feb 23rd, 2010, 04:20 AM
Why not use absolute paths to images and CSS?

http://webdesign.about.com/od/beginningtutorials/a/aa040502a.htm

For example:

Instead of ../../css/screen.css
Use /css/screen.css

I don't think this is a .htaccess problem, it's a problem with your paths to your resources.

kows
Feb 23rd, 2010, 08:14 AM
yeah, I'd do what ILMV suggested. either store a constant that defines an absolute path (including your domain), or use the types of absolute paths that ILMV described.

I'm sure there's something you might be able to do that captures anything with a "css/" or "img/" directory and then rewrites to the correct directory, but it's a little unnecessary when you can solve the problem so easily by prepending a slash onto your URLs.

I_Love_My_Vans
Feb 23rd, 2010, 09:08 AM
Many years ago (before I knew about absolute URLs) I used a crazy complicated bit of PHP to calculate how many ../'s I needed for my resource links.

Not only are absolute URLs a bajillion times more effective but it increases portability, say you want to copy your design to a sub domain, if your resources directly refer to the domain they will be useless under a different address.

In my current job we are being ultra cautious to make our project as portable as possible, because we don't know if one of our partner organisations are going to want this in a sub domain of their corporate URL, or if their going to want to use Oracle instead of PostgreSQL.

Think portability!

TheBigB
Feb 23rd, 2010, 04:54 PM
Maybe I should've added that I'm building a little cms with templates and all.

Portability was pretty much the issue.
Imagine I'd have a complete site in a subdirectory which I do in localhost;
http://localhost/foo/bar.php
For the template, hardcoding in the URL would be a no-go as that would vary on systems and pretty much destroy the portability.

But I've worked out that the base path should be a general setting so I can glue that in front of the url's that would fail otherwise.
(I'm having difficulties building this sentence, hope you understand it :afrog:)

Anywany, thanks for the input! :D

penagate
Feb 23rd, 2010, 05:14 PM
Yes, the typical solution is to have a base URI for your product, which is itself an absolute URI.

There's also a HTML base tag, which has its own set of pitfalls (http://mondaybynoon.com/2006/11/13/the-pros-and-cons-of-the-base-tag/).

TheBigB
Feb 23rd, 2010, 05:26 PM
Although I don't give a sh about IE6 users, I think it should be done server-side.
But nice tip, didn't know that one.