
Question:
I have made use of a simple rewrite rule in my .htaccess file to make my URL's pretty. What was once like this:
http://somedomain.com/product-details.php?product=MQ==&hash=123456789
Is now this:
http://somedomain.com/secure/123456789/MQ==/rings/details/blue-diamond-topaz-ring.php
This is all very well, but I have many a PHP file that has all JS and CSS links as relative. Because of the above new pretty URL, these links to JS and CSS are now 5 or 6 directories too deep.
<strong>My Question:</strong> By means of mod_rewrite, is there a way I can make a request for css/style.css
or js/somescript.js
rewrite to something like http://www/somedomain.com/css/style.css
for example?
I have Googled my heart out and done my homework, and while I can find a million references to rewriting URL's, none seem to work in the same manner for requesting such scripts. I tried this but to no avail:
RewriteRule ^/css/(.*)$ http://localhost/dev/css/$1 [R=302,L]
I assumed that would send ALL requests like /css/somefile.css
to http://localhost/dev/css/somefile.css
Any help or guidance is well and truly appreciated.
<hr /><strong>UPDATE: Here is the rewrite portion of my .htaccess file:</strong>
RewriteEngine on
Options +FollowSymLinks
RewriteBase /eterniti-mygate/
RewriteRule ^([^-]*)-article-([^-]*)\.php$ /in-the-news.php?action=$1&article=$2 [L]
#rings rewrite rule
RewriteRule ^secure/([^-]*)/([^-]*)/rings/details/([^_]*)\.php$ ring-details.php?product=$2&hash=$1 [L]
RewriteRule ^css/(.*)$ http://localhost/eterniti-mygate/css/$1 [R=302,L]
<strong>UPDATE: Here is the log file output for the rewrites</strong>
Please see my log file at <a href="https://docs.google.com/leaf?id=0B-NRfZvE6xi7MmI0N2ZjNzEtYWZlYy00NjJiLWJhMjQtYzAyYzY4ZWIxN2U0&hl=en_GB" rel="nofollow">https://docs.google.com/leaf?id=0B-NRfZvE6xi7MmI0N2ZjNzEtYWZlYy00NjJiLWJhMjQtYzAyYzY4ZWIxN2U0&hl=en_GB</a>
It appears to not be rewriting the request from the base, but rather from the virtual directory (eg /secure/ring/details/). I have set the rewriteBase to /eterniti-mygate/ which is a folder on my wamp server root.
Answer1:When using rewriterule remove the leading (back)slash, so try this:
RewriteRule ^css/(.*)$ http://localhost/dev/css/$1 [R=302,L]
Answer2:This should be useful
RewriteCond %{REQUEST_FILENAME} \.css
RewriteRule ^css/(.*)$ http://localhost/dev/css/$1 [L]
When a .css file is called, you rewrite (if the file is under css/) to http://localhost/dev/css/<pathorname>
.
Take care that <pathorname>
can include subdirectories.
Also the [R=302] might just not be needed. Internal redirection should be enough.
If that does not work, you can see what happens with :
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 2
(you can push log level to 4 if you still can't understand.)
<h2>' Edit with the log details</h2>Line 20 : [perdir C:/wamp/www/eterniti-mygate/] add path info postfix: C:/wamp/www/eterniti-mygate/secure -> C:/wamp/www/eterniti-mygate/secure/af192eaa6808acab8947ce59a32f8d17/MQ==/rings/details/css/reset.css
Line 21 : [perdir C:/wamp/www/eterniti-mygate/] strip per-dir prefix: C:/wamp/www/eterniti-mygate/secure/af192eaa6808acab8947ce59a32f8d17/MQ==/rings/details/css/reset.css -> secure/af192eaa6808acab8947ce59a32f8d17/MQ==/rings/details/css/reset.css
We are trying to serve secure/af192eaa6808acab8947ce59a32f8d17/MQ==/rings/details/css/reset.css
'^([^-]*)-article-([^-]*)\.php$'
'^secure/([^-]*)/([^-]*)/rings/details/([^_]*)\.php$'
'^css/(.*)$'
Won't match. Knowing that the caret (^
) means : beginning of the string/URL (http://mysite.com excluded
), you can't detect something that is not like css/<something>END
(FYI: $
means ends of string/URL). Remove it from your rule and it should match <anything>css/<something>END
and the rule will apply.