Converting relative URL requests to absolute URL request using mod_rewrite


I have made use of a simple rewrite rule in my .htaccess file to make my URL's pretty. What was once like this:


Is now this:


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.


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

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


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.


  • android 2.2 WebView and WebViewClient RTSP problem
  • SecurityError for same-origin image texImage2D
  • Cannot get ComboBox selected item value
  • How can I install and use latest Git on Mac OS X 10.9?
  • iOS UIWebView: How can I figure out what the previous URL is?
  • Newtonsoft inline formatting for subelement while serializing
  • Why is RAM in powers of 2?
  • Visual basic auto imports namespaces
  • Where can I get runkit DLL extensions for PHP 5.3+?
  • .htaccess mod rewriterule and ampersands
  • How to work with multiple CSS properties?
  • htaccess settings for angularJS pushstate
  • How to read JSON-LD data from HTML in Objective-C?
  • WPF Listbox commands
  • Mod rewrite redirection to another domain if file not exist
  • PHP UTF-8 to GB2312
  • Classic ASP URL Rewriting
  • to implement a spinner in angular2+
  • What's the name of this finding square root algorithm?
  • python: forcing relative imports to search from script file
  • CORS with socket.io
  • mave 3.2 not able to access local nexus instance return 502 code
  • Python 3.2.2, error(scripts to exe)
  • Examples of how to a STS in .Net 4.5 using WCF
  • perl, mysql - fasting way to upload a csv file into mysql?
  • Tomcat memory Leak
  • Apache RewriteRule redirection with url encoded
  • htaccess add www if not subdomain, if subdomain remove www
  • Installing iPhone App to iPhone
  • How to attach a node.js readable stream to a Sendgrid email?
  • Test if a set exists before trying to drop it
  • Cannot resolve symbol 'MyApi'
  • Handling un-mapped Rest path
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Arrays break string types in Julia
  • Comma separated Values
  • Append folder name and increment by 1 using batch script
  • How to load view controller without button in storyboard?