0

Got htaccess rewriting my links for better seo like this:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  .*/([^/]+)/?     [NC]
RewriteCond %{REQUEST_URI}  !index\.php      [NC]
RewriteRule  .*      /index.php?cat=%1 [L,NC,QSA]

which rewrites

http://www.example.com/any/number/of/directories/lastDir

to:

http://www.example.com/index.php?cat=lastDir

but my css isnt working, when i upload htaccess to the server, there is just plain text without images and css

tried adding base tag into html, but its not working

<base href="http://www.example.com/">
2
  • Try using .*/([^/.]+)/? instead. Your rule is probably matching the css and image file. Don't allowing dots in the rewrite would prevent this, since the file-extension contains a dot. Commented Mar 11, 2013 at 19:15
  • not working for example.com/aaaa, only working for example.com/aaa/bbb and so on Commented Mar 11, 2013 at 19:32

3 Answers 3

2

You must exclude the css and images from being rewritten with a RewriteCond

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !\.(?:css|png|jpe?g|gif)$ [NC,OR]
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ([^/]+)/?$ /index.php?cat=$1 [QSA,L]
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for jpe?g, never thought of that before, i always use jpeg|jpg and have been racking my brains for something neater and cleaner
@bizzehdee Thank you, I adopted it from somewhere else. Nothing wrong with jpeg|jpg, though.
1

try using

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  .*/([^/]+)/?     [NC]
RewriteCond %{REQUEST_URI} !^(index\.php|robots\.txt|img|font|js|css|scripts) [NC]
RewriteRule  .*      /index.php?cat=%1 [L,NC,QSA]

this will filter out certain file types and directories to make sure that assets are not redirected

Comments

1

I was having this problem as well and found a solution.

If your css / js files are linked with a relative path, your RewriteRule will also affect them. Instead of writing a RewriteCond, you can avoid this by using a path from the root directory. In other words:

<link type="text/css" rel="stylesheet" href="css/style.css">

becomes

<link type="text/css" rel="stylesheet" href="/css/style.css">

Where css is a directory in the root directory.

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.