Kedor
Kedor

Reputation: 1498

Mod_rewrite - images, styles and scripts missing

On the start i want you to take notice, i did try finding solution in google, or in stackoverflow. I found some tips, but they didn't work for me, or i simply failed following instructions.

So, here is my site directories:

www.domain.pl/index.php    <--index file
www.domain.pl/images/      <--images folder
www.domain.pl/styles/      <--styles folder, for now just 1 css file
www.domain.pl/script/      <--scripts folder, js files, and 1 php file called with include
www.domain.pl/font/        <--font files

Now, when i open my website with www.domain.pl or www.domain.pl/index.php it works like a charm. But i want to be able to add some parameter for example www.domain.pl/index.php?action=dosmth, but it doesn't look good for a users, so thats the place where I wanted to use mod rewrite.

Now comes tricky part, i want it to look like www.domain.pl/index/actionparam/ but when i do that, I get error in console, saying my images, scripts, and css cant be loaded/found. (i tried rewriting it like that: RewriteRule ^index/([^-]+)/script/jquery-1.7.1.min.js$ script/jquery-1.7.1.min.js [L] but I cant use it for every single image, script, css, or font file. There need to be better way.

So, my mod rewrite so far:

Options FollowSymLinks 
RewriteEngine On 
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.pl/$1/ [L,R=301]

RewriteRule ^index.html$ index.php [L] 
RewriteRule ^index/$ index.php [L] 
RewriteRule ^index/([^-]+)/$ index.php?action=$1 [L] 

First part, i am trying to add "slash" at the end of url, if user didnt put it there. http://domain.pl/index->http://domain.pl/index/ or http://domain.pl/index/myaction->http://domain.pl/index/myaction/

Question
Is it possible to somehow skip folders with images, scripts etc when rewriting it? Or how could i rewrite all images at once?

Additional question
http://domain.pl/myaction/ redirect straight to http://domain.pl/index.php?action=myaction right away, without need of putting /index/ in there? Yes, i am learning mod rewrite.

Upvotes: 1

Views: 479

Answers (1)

Felipe Alameda A
Felipe Alameda A

Reputation: 11809

You don't have to include index in the request, only the parameter. Example:

Request: http://domain.pl/myaction

Options +FollowSymlinks -MultiViews
RewriteEngine On 
RewriteBase /

# Exclude existing files and directories.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* -  [L]

# Get the parameter and pass it to index.php
RewriteCond %{REQUEST_URI} !index\.php  [NC]
RewriteRule ^([^/]+)/?  /index.php?action=$1  [L,NC]

Upvotes: 2

Related Questions