Rewrite rule include paths with slashes not working
I have an old website and I want to upgrade it and let it use .htaccess Rewrite rules for search engines, for example, let's say that I have this rule in my .htaccess file
RewriteRule ^category/electric category.php?type=8
this will work without any problems but when the page loaded no images will appear and no css is applied, afte开发者_如何学编程r I did some search on the internet I found that this problem because of the URL being like this:
http://www.mywebsite.com/category/electric
and I found the solution but it will take a lot of time to apply it, I found that if I put the full url in each element that require paths like JS, CSS, images, ...... files every thing will be OK like this
<img src="images/image.png" width="100" height="100" alt="" />
to
<img src="http://www.mywebsite.com/images/image.png" width="100" height="100" alt="" />
but this will take time as I said so is there any way to cover this problem with any solution.
and sorry for bad English
You can just use the full URL path to the image, like this:
<img src="/images/image.png" width="100" height="100" alt="" />
Note the /
in front of images
: that makes the path relative to the document root, not to the current directory.
Another option is to add a <base>
tag into the <head>
of your pages, like this:
<base href="http://www.mywebsite.com/" />
This will make all relative URLs on the page act as if it was located at http://www.mywebsite.com/
.
Unfortunately, both of these solution do require editing the HTML. HTTP/1.1 does define a Content-Location
header which is supposed to act like the <base>
tag in HTML, but browser support for it seems to be limited. (For example, Mozilla seems to have decided not to support it because it would break too many existing sites.) Apparently, some versions of the HTTP spec also had a Content-Base
header, but it was also poorly supported and was later removed from the standard.
Yet another option, of course, is to make images/image.png
work even if prefixed with category/
. The following rewrite rule should do it:
RewriteRule ^category/images/(.*) images/$1 [L,R=permanent]
(An internal redirect should also work, but may result in browsers requesting the images twice because they don't realize they're the same. If the images are very small, it might still be more efficient.)
You could try a rewrite rule like:
RewriteRule ^.*category/electric category.php?type=8
Note the extra ".*" in the beginning; that's a wildcard match, which means it should allow any URL, as long as it ends with category/electric...
RewriteRule ^.+(/images/.*)$ $1 [R=301]
Redirects example.com/category/images/example.jpg to example.com/images/example.jpg
Not ideal as it requires an extra http request for every image, but there is not other way other than changing the html.
精彩评论