1

I was working with URLs on my webpage but I can't solve issue for URLs with 2 parameters.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-z\-]+)/?$ index.php?strona=$1 [L]
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/?$ index.php?strona=$1&id=$2 [L]

URLs seem fine except that when my current URL has 2 parameters (for example I'm on http://example.com/subpage/5 whole webpage is broken (stylesheets, navigation etc) because .htaccess changed all links to:

(for example navigation):
http://example.com/subpage_with_2_parameters/home
instead of
http://example.com/home

Pages with one parameter (example: http://example.com/contact) work fine.

Only solution (which is horrible) I have on mind are absolute links.

2 Answers 2

4

You're not the only one dealing with this problem of css, js, images paths getting messed up after implementing so-called pretty URLs. I am seeing these problems being reported on SO almost every day.

You can solve this problem in 3 ways:

  1. Best solution is to use absolute paths for images, css and js files i.e. start your path with / or http://

  2. Another option is to use base href tag in HTML head section like this:

    <base href="http://www.example.com/">

  3. 3rd option is via mod_rewrite

Put these lines above your other RewriteLine in your .htaccess file:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^[^/]+/([^.]+\.(?:js|css|jpe?g|png|gif))$ /$1 [L,R=301,NC]
Sign up to request clarification or add additional context in comments.

Comments

0

It's not your rewrite rules which are breaking your stylesheets, but your actual HTML. The same thing would happen if you had an actual directory called foo and placed index.php in there.

If you write <a href="home">Home</a>, that link is relative to the current directory (in URL-space) so on a page with a URL like http://example.com/foo/bar/baz it links to http://example.com/foo/bar/home.

What you want instead is for the link to be relative to the root of your domain; for that, you need a leading slash: <a href="/home">Home</a>

The only reason this seemed to work before is that all your URLs were in the root directory, so "current directory" and "root of domain" were the same thing.

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.