Page 1 of 1

mod-rewrite

Posted: Wed Jun 28, 2006 10:12 am
by Ollie Saunders
Here's my .htacess file as it is, the comments describe what i'm trying achieve:

Code: Select all

RewriteEngine on

# rewrite something like "www.bob.com/any/number/of/directories/css/file.css" to "www.bob.com/css/file.css"
RewriteRule css/(.*) /css$1
# do the same for js, gfx and file
RewriteRule js/(.*) /js$1
RewriteRule gfx/(.*) /gfx$1
RewriteRule file/(.*) /file$1
# everything else should be redirected to index.php which is in the document root
RewriteRule !(css|file|js|gfx)/ index.php
I've played around with about ten variations of this and I either get internal server errors or it doesn't do what I want. I have a feeling I need to use RewriteConds but you don't seem to be able to test them against the standard request string.

Any help will be greatly appreicated.

Posted: Wed Jun 28, 2006 10:32 am
by RobertGonzalez
Your rewrite rule need to take A and translate that to B. You said you wanted people who go to http://www.bob.com/any/number/of/direct ... s/file.css and have them actually get served http://www.bob.com/css/file.css. So you tell your .htaccess file to do something like this...

Code: Select all

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase / 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
# Skips the following S rules is they do not apply 
RewriteRule ^.*$ - [S=1] 
RewriteRule ^(.+)/css/file.css$ /css/file.css [QSA,L] 
</IfModule>
Basically it needs to look at the url, regex everything that could contain */css/file.css and rewrite that to /css/file.css.

Take a look in the Apache, IIS, Web Servers forum, there is a sticky called Useful Resources that has a mod_rewrite cheatsheet that might be able to offer some assistance.

Posted: Wed Jun 28, 2006 4:11 pm
by Ollie Saunders
Well thanks for your response, someone else solved it for me with this:

Code: Select all

RewriteEngine On

# /anything/css/file.css -> /css/file.css
# directories are seperated by a | in the second ()
RewriteRule ^.+/(css|file|js|gfx)(/.*)?$ /$1$2 [L]

# ignore directories (from above)
RewriteRule ^(css|file|js|gfx)(/.*)?$ - [L]

# if it's not /index.php send it to /index.php
Rewriterule !^index\.php$ /index.php [L]

Posted: Wed Jun 28, 2006 4:14 pm
by RobertGonzalez
Cool. Glad you got it figured out.

Posted: Wed Jun 28, 2006 4:38 pm
by Ollie Saunders
Yeah. Its going well.

Thanks for your help anyways.