mod-rewrite

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

mod-rewrite

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Cool. Glad you got it figured out.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Yeah. Its going well.

Thanks for your help anyways.
Post Reply