Image and css not displaying while redirecting using htaccess
When i redirect a file using htaccess the images and css, javascript are not applied correctly.
for example, i've redirected the page,http://example.com/
to
http://example.com/au/
for this i've use the following code in htaccess,
RewriteRule ^([a-z]{2})/?$ index.ph开发者_Go百科p?cn=$1
Now the pages are redirected correctly but not the css,images. what could be the issue? I've files like this,
/index.php
/style.css
/images/...
/.htaccess
Edit: <link rel="stylesheet" type="text/css" href="/style.css" />
Make sure that you don't have any absolute URL problems. When working with htaccess sometimes you may forget that the URL the browser thinks its visiting (a URL you've rewritten) doesn't necessarily directly correlate with the file hierarchy in your web root. I always recommend using absolute paths relative to the site's www root when dealing with external content. For example the proper link to load the style.css file would be:
<link type="text/css" rel="stylesheet" href="/style.css" />
And not...
<link type="text/css" rel="stylesheet" href="style.css" />
This way when the browser thinks it's in the au sub directory or even when its somewhere else, CSS, JavaScript, and image files can be loaded without having to specialize URL's based on the rewrite's URL.
You have to add a RewriteCond
before the rule, to make sure that any files that actually exist (e.g. - style.css
) don't get passed to index.php
Try adding this bit of code before your existing RewriteRule
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
精彩评论