Converting relative URL requests to absolute URL request using mod_rewrite

喜夏-厌秋 提交于 2019-12-12 01:49:41

问题


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.

My Question: 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.


UPDATE: Here is the rewrite portion of my .htaccess file:

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]

UPDATE: Here is the log file output for the rewrites

Please see my log file at https://docs.google.com/leaf?id=0B-NRfZvE6xi7MmI0N2ZjNzEtYWZlYy00NjJiLWJhMjQtYzAyYzY4ZWIxN2U0&hl=en_GB

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.


回答1:


When using rewriterule remove the leading (back)slash, so try this:

RewriteRule ^css/(.*)$ http://localhost/dev/css/$1 [R=302,L]



回答2:


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.)

' Edit with the log details

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.



来源:https://stackoverflow.com/questions/6731564/converting-relative-url-requests-to-absolute-url-request-using-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!