Attempting to access a shared php/javascript library using mod_rewrite

Let's dive into a fresh perspective on a question I previously raised:

I've crafted a mod_rewrite snippet that checks for the existence of JavaScript, CSS, and PHP files on the subdomain they are called from (e.g., subdomain.example.com). If these files don't exist, Apache "redirects" to common.example.com, where common JS, CSS, and PHP files reside. Here is the code snippet:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.(js|css|php)$ http://common.example.com/$1.%2 [L]

This setup functions as intended when:

  • You manually input the file name in the address bar.
  • The JS or CSS files are referenced in the HTML markup.

However, it encounters issues when:

  • The URL is utilized within a PHP or JS file (e.g., during an AJAX call), leading to a reference back to the subdomain hosting the HTML page (i.e., mod_rewrite fails to catch it): subdomain.example.com

Thank you to the community for your assistance.

UPDATE

Following Pekka's request, here's an illustrative scenario:

  1. Consider subdomain.example.com/test.html.
  2. test.html calls
    <script src="/code/js/test.class.js" type="text/javascript" ></script>
    .
  3. In test.class.js, there's
    $.ajax({ url: "/code/php/test.php", dataType : "json", data: {cmd : 'someCommand'}, success: callback()});
    .

..

  • Hence, at #2, it retrieves
    common.example.com/code/js/test.class.js
    (Correct!).
  • While at #3, the AJAX request points to
    subdomain.example.com/code/php/test.php
    (Incorrect due to subdomain).

Answer №1

This is a consequence of altering the domain structure and making changes to the protocol within the rewrite process.

When you input a complete URL in your RewriteRule instead of a file path, the server is compelled to issue a 301 redirect header for each request.

Typically, AJAX requests will adhere to header redirects, however in this case, redirection is occurring to a distinct domain, which breaches the Same Origin Policy!

Consequently, this approach cannot be utilized for Ajax requests. It would be necessary to specify a file path in your RewriteRule to allow for internal rewriting (resulting in the browser perceiving it as

subdomain.example.com/code/php/test.php
). I am uncertain if this is feasible within your configuration.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Foreign Key in AJAX POST method with Django

I'm encountering difficulties with my web app as I try to specify a user in my Ajax request. Whenever I attempt to add a new item, an error pops up: TypeError: Field 'id' expected a number but got . Is there any solution to this issue? Her ...

Refreshing Appended Content Using JQuery AJAX

Here is the script I'm working with: <script> $(document).ready(function() { $('#signUpForm').submit(function() { $.ajax({ type: 'POST', url: "process.php", data: $("#signUpForm").serialize(), success: function(d ...

The update functionality of the p:commandButton seems to be malfunctioning when used within a ui:include

Looking to refresh part of a page using PPR (Partial Page Rendering). This section of the page needs updating: <h:panelGroup id="aggiungiAuto" rendered="#{!autoBean.operazioneOk}"> <ui:include src="../component/aggiungi_ ...

After removing the timezone from the timestamp log, why is it now showing as one day behind?

Within my programming, I store a timestamp in the variable 'var timeStamp = finalData.obj.followers[0].timestp;', which currently outputs '2020-04-15T00:00:00.000Z.' To extract only the date and remove the time zone information, I util ...

Upon initiating a React Native project, five critical vulnerabilities become apparent

Upon starting my React Native project, I encountered 5 high severity vulnerabilities. Despite attempting to fix them with the command "npm audit fix --force", the issue persisted. I even went as far as reinstalling node.js and React Native, but the vulne ...

Every time I launch my express application, I encounter this error message

Encountering a type error with Express Router middleware. See below for the code snippets and error details. Any assistance is appreciated? The application is functioning properly, but when attempting to access the URL in the browser, the following error ...

PHP Error: Fatal error - Insufficient memory (allocated 576192512) attempted to allocate 486801418 bytes in "file" at line 410

Here is a snippet of the program that is causing the issue: file_put_contents("icecatcategorydata.xml.gz", $xml); $file = gzopen("icecatcategorydata.xml.gz", "r"); $content = ''; while (($buffer = fgets($file)) !== false) { $content .= $buff ...

A guide to efficiently passing props in Quasar 2 Vue 3 Composition API for tables

I am encountering an issue while attempting to create a custom child component with props as row data. The error message "rows.slice is not a function" keeps appearing, and upon inspecting the parent data, I found that it is an Object. However, the props r ...

Is there a way to grab code from a different HTML page using JavaScript and jQuery when the user clicks on something?

After testing various codes for the intended purpose mentioned in the title, I have observed a peculiar behavior. The code from settings.html appears briefly and then disappears rapidly. Javascript: function load() { $("#paste").load("settings.html") ...

The popularity of AJAX in JavaScript is continuing to rise

I am facing an issue with my website that features a configurable 3D object with various properties. Whenever I reload the div containing the 3D object to reflect new properties, the script data keeps adding on. This not only slows down the functionality a ...

What is the best approach to tackle this design?

As a newcomer to React, I'm facing challenges in positioning components. The layout I envision is consistent across all pages, with certain elements remaining the same. Should I utilize a grid system for this design? How should I approach achieving th ...

Ways to have a function return a promise from the final "then" in a series of promises

I am delving into the world of test automation with Selenium and JavaScript. As a newcomer to both, along with functional programming and promises, I am facing a challenge in creating a function that performs three essential tasks: Click on an input Clea ...

How to implement multiple dependent select boxes in Rails?

I'm currently working on developing a car application where each car is assigned to a specific make and model. However, not all makes have every model available. My goal is to create a series of select boxes that update dynamically based on the selec ...

Automatically refresh the page after submitting

Being relatively new to PHP and still learning, I encountered a problem with my current project. I am in the process of creating a page where I can store my favorite websites using the GET method to send the Title, URL, and Logo of the website to the serve ...

Initiating an AJAX call to the Twitter API 1.1 search using jQuery

Check out this easy example of accessing Twitter's search API to retrieve tweets associated with a hashtag that is known for having a lot of activity, like #fml. I think I am using the application-only authentication properly according to this guide: ...

Connecting a Router with CakePHP

Router::connect( 'mylogin', array('controller' => 'User', 'action' => 'xyz', 5) ); Is there a method for transforming the string '/User/xyz/5' stored in a database into an array arr ...

Personalized JSON response type for AJAX requests

Do you think it's possible to achieve this? I have an idea to create a unique dataType called "json/rows". This new dataType would parse the server output text and manipulate it in some way before passing it to the success function. What do you think ...

Wrapping an anchor tag with a div in Codeigniter

Can a div tag be used inside an anchor function? I have a div with the following CSS: #first{ opacity:0; } Now, I want to include it in my anchor element. Here is the code snippet: <?php if(is_array($databuku)){ echo '<ol>&l ...

Contemplating the protection of an Android online platform

In the process of developing an Android app to connect to a http server, I have implemented a web service that sends JSON data to a PHP script on the server. This PHP script then accesses the desired database and inserts the JSON objects. However, my focu ...

Managing ajax requests, failing to retrieve information

I am struggling to configure my client-side ajax calls to send data to a node express server. I want the ajax request to be triggered "onclick" of an href link. My goal is to pass the ID of the link as a variable to the server, but unfortunately, the serv ...