Forum Moderators: phranque
I'm entirely new to mod_rewrite. I went through this forum and I've managed to obtain a little of what I expected. But still I'm stuck with the relative url paths.
I'm not using any .htaccess file. Instead I'm including the rewrite rules in <IfModule rewrite_module> of httpd.config file, which seems to be working.
Here is a part of it.
I want to rewrite the url, [localhost...] to [localhost...] So I've written the rewrite rule as
RewriteEngine on
RewriteLog "logs/rewrite.log"
RewriteLogLevel 9
RewriteRule ^/WARFILE/([A-Z]*)/([0-9]+)$ /WARFILE/PostReview.do?id=$2 [R=301]
The rule redirects the to the required page, but it shows the new url in the address bar(PostReview.do?...). So I changed the options flag to [PT]. Now the page is getting redirected with the old url, but the relative paths in the page is not working(images, css etc). In my jsp page the src for images are like src="images/1.gif" or for js file its, src=".js/review.js"
I've set my Document Root to the deploy foler, i.e "C:/getveq_soft/jboss-4.0.5.GA/server/getveq/deploy/WARFILE.war" and Directory tag is as follows:
<Directory "C:/getveq_soft/jboss-4.0.5.GA/server/getveq/deploy/WARFILE.war">
# Options Indexes FollowSymLinks
Options +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I've 5 set of rules of the same category. However, the result is same in all cases.
Please help me in getting rid of this problem.
Thanks,
Praveen
1) You want an internal rewrite, not an external redirect. An external redirect sends a response to the browser (or SE robot) saying, "That resource has moved, ask for it at this new URL)." So the browser updates its address bar and asks again.
An internal rewrite says, "If you get a request for this URL, go to that filepath to service the request." This occurs entirely within the server, and is not visible to the client browser or robot.
The proper form for an internal rewrite would be:
RewriteRule ^/WARFILE/[A-Z]{2}/([0-9]+)$ /WARFILE/PostReview.do?id=$1 [L]
(I assumed that the "NJ" represented a two-letter state code, and made the pattern specific to that.)
2) It is the browser, not the server, that resolves relative links (use a server headers checker such as the "Live HTTP Headers" add-on for Firefox/Mozilla browsers to verify this). The browser resolves relative links by using the "directory location" of the URL it currently has in its address bar. Since you have told the browser (and the world) that the directory location is /WARFILE/NJ/, all relative links will be resolved based on the directory, and not the /WARFILE/ directory where your PostReview script is located. Therefore, these links will be broken.
You have a couple of choices: You can either change your page-relative links ( <img src="images/logo.gif"> ) to server-relative ( <img src="/images/logo.gif"> ) or canonical ( <img src="http://example.com/images/logo.gif"> ) URLs, or you can detect incorrectly-resolved relative links and rewrite them to the correct location. However, this second option results in multiple URLs for the same thing, and is not recommended.
In simple terms, it is the URL in the address bar that tells the browser "where it is" and it will resolve relative links that it sees on your pages according to that location. By adding the "virtual directory" /NJ into the path, you have changed the base location that the browser will use to resolve relative links.
A comment: If possible, change that "WARFILE" to something else. It sounds "scary" and believe it or not, you may lose some traffic because of it. Make it "friendlier-sounding".
Also, I strongly recommend that you do not use uppercase or mixed-case URLs; These can be a nightmare if you ever have to implement code to fix incorrectly-cased incoming links. In the case where your URLs are all lowercase, this can easily be done by using RewriteMap to call the system "tolower" function. In fact, the same is true if you adopt all-uppercase URLs, but those are "ugly."
However, if you use mixed-case URLs, no such easy solution is available. You would either have to handle each and every possible case variation of each and every mis-cased link as a separate rule, or take each URL and look it up in a database (using RewriteMap again) to find the correct-cased version.
And this problem is likely to occur frequently, since mixed-case URLs will make linking errors on sites that link to you, and even on your own site much more likely. Stick with all-lowercase to prevent massive frustration...
Jim
I also tried creating a .htaccess file and placed it inside the war folder. (ole.war - I've renamed the war file as per your advice). But not only the images, even the page did not load.
I read about RewriteCond in the forum and I placed the following lines before the Rewrite rules which looks like:
RewriteCond %{REQUEST_URI} !(.*)\.gif
RewriteCond %{REQUEST_URI} !(.*)\.png
RewriteCond %{REQUEST_URI} !(.*)\.jpg
RewriteCond %{REQUEST_URI} !(.*)\.jpeg
RewriteCond %{REQUEST_URI} !(.*)\.css
RewriteCond %{REQUEST_URI} !(.*)\.js
RewriteRule ^/ole/[A-Z]{2}/([0-9]+)$ /ole/PostReview.do?businessId=$1 [PT,L]
RewriteRule ^/ole/[A-Z]{2}/([0-9]+)/details$ /ole/PostReviewDetails.do?businessId=$1 [PT,L]
But still the images, css and js files are pointing to the url "http://localhost/ole/NJ/images/profile/summary_on.gif".
But what I need is "http://localhost/ole/images/profile/summary_on.gif".
I think the RewriteCond has gone wrong syntactically but not conceptually. Or should I go ahead with htaccess file? Please correct me where I was wrong.
Thanks,
Praveen.
RewriteCond $1 \.gif$ [OR]
RewriteCond $1 \.png$ [OR]
RewriteCond $1 \.jpe?g$ [OR]
RewriteCond $1 \.css$ [OR]
RewriteCond $1 \.js$
RewriteRule ^/ole/[A-Z]{2}/images/(.+)$ /ole/images/$1 [PT,L]
Jim