5

I'm trying to make mod_rewrite the first sub-directory string from url in order to create similar functionality as 'jsfiddle.net saved url's within a class/db. The script works fine and does the rewrite.

e.g. of url

http://jsfiddle.net/RyEue/

This works fine (loads all css, scripts, etc.):

http://www.domain.com/787HHJ2

This is what I've used in the past which does the trick.

The problem Is when ending URL with last slash, script, css and others loose path.

http://www.domain.com/787HHJ2/

rewrite script:

DirectoryIndex index.php index.html
Options +FollowSymlinks
RewriteEngine On # Turn on the rewriting engine
#RewriteBase   /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !.
RewriteRule ^.+/?$ index.php [QSA,L]

Unsure if this has to do with Rewritebase, I've tried multiple ways.

PS. I've tried setting paths to absolute (e.g. src="/img/theimage.jpg") without luck.

7
  • The rule is fine. Please turn on rewrite debugging (RewriteLogLevel 9) and check rewrite log to see exactly what is going on. Commented Jul 23, 2011 at 15:51
  • @LazyOne, Your right. It works. The issues is that it breaks css, scripts paths. Commented Jul 23, 2011 at 16:01
  • 1
    I've clarified the question. The problem is mostsly with the css, scripts, images and others been broken upon using the trailing /. Commented Jul 23, 2011 at 16:14
  • What do you mean by "breaks"? Try %{DOCUMENT_ROOT}%{REQUEST_URI} instead of %{REQUEST_FILENAME}. BTW, what RewriteCond %{QUERY_STRING} !. mean -- only if query string is empty? Commented Jul 23, 2011 at 16:17
  • By "breaks" i mean the html elements like img do not load after rewrite. Yes, only is query string is empty. I'm trying to display index.php with all it's elements. Commented Jul 23, 2011 at 16:31

2 Answers 2

5

1. Make sure you have css/images/js etc linked relative to root folder (with leading slash): /styles/main.css

2. Add one of these ruls before current one:

# do not touch files with .css/.js/.jpg etc extensions
RewriteRule \.(css|js|jpg|png|gif)$ - [L]

or

# do not touch any resources in images/css/js folders
RewriteRule ^(images|css|js)/ - [L]

3. Clear browser caches and restart (sometimes browser may display cached page/resource when rewrite rule was fixed which brings a lot of confusion).

Sign up to request clarification or add additional context in comments.

1 Comment

With the above answer I was able to solve the problem. A combination of relative paths to root plus setting up local virtual hosts allowed everything to work perfectly.
0

Try escaping

RewriteRule ^.+\/?$ index.php [QSA,L]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.