Investigating the presence of a file within the superior directory with RewriteCond

I am attempting to enhance this .htaccess file in order to redirect all requests to a script when the requested file or directory is non-existent. However, I want to refactor it so that it checks for the existence of the file in the parent folder.
Currently, my .htaccess looks like this:

RewriteEngine on

# check if requested file or directory exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# map requests to contentpage.php and append it as a query string
RewriteRule ^(.*)/(.*)$ contentpage.php?url=$2&lang=$1

At present, this .htaccess successfully redirects
en/products.php -> contentpage.php?url=products.php&lang=en only if there is no products.php file within the child directory /en/ (and since the physical directory /en/ does not exist, the RewriteCond fails to filter cases where I have a file named products.php in the parent directory).

I need to modify it so that it checks for the file's existence in the top-level directory where my files are located. The redirection should only occur for language directories such as /en, /es, /de, /pt, etc.

Does anyone have any ideas on how to solve this issue? Thank you.

Answer №1

If you want to apply specific rules for your website, you can create an htaccess file in the root directory. This allows you to customize the behavior of your URLs.

 /parent
     /en
     /es 
     /de

Whether you access the parent path or any of the child paths:

  • /parent/products.php
  • /parent/en/products.php

The directives written in the /parent/.htaccess file will be applied.
So, you have the freedom to define your own set of rules:

# Check if the requested file does not exist in the child directory.
# If it doesn't, then check if it also does not exist in the parent directory.
RewriteCond /parent/$1/$2 !-f
RewriteCond /parent/$2 !-f
RewriteRule ^/parent/(es|en|fr|de)/(.*)$ contentpage.php?url=$2&lang=$1

# Check if the requested file does not exist in the parent directory.
RewriteCond /parent/$1 !-f
RewriteRule ^/parent/(.*)$ contentpage.php?url=$2&lang=$1

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

Error 404 when Making an Ajax Request in Yii2

I'm trying to implement AJAX in Yii2 (a PHP framework) but I am facing issues with the following code. This is my view file (PHP): <script> var url1='<?php echo Url::toRoute('Agehi/Ajaxshahr'); ?>'; </script> & ...

Transferring an organized array to processing

My goal is to transfer an array of integers from a php file called load.php to a JS script, which will then forward it to a Processing file written in JavaScript. In load.php, I define the array and send it using JSON (the array contains a minimum of 40 i ...

Strategies for managing multiple openIDs linked to a single user account

Currently, I am implementing a login system on my website similar to the one used on SO. Users have the option to log in with their Facebook, Google (Gmail openID), or Twitter accounts. This inquiry does not focus on specific oAuth or OpenID implementatio ...

Guide to sending ajax content to a controller in codeigniter

Having trouble posting data to the controller in CodeIgniter for autocomplete functionality with a database. Struggling to send typed or selected data from the view to the controller using AJAX. Below is the AJAX function: var i=$('table tr').le ...

determining the percentile for a vast dataset

My database holds student information related to test codes and the marks they achieved for each test. I am looking to recalculate percentile marks for students corresponding to each test code. Although I have a code for a series of test codes, it is not ...

Is it possible to include both the value and text of a dropdown in form.serialize?

For my project, I need to serialize the form data. However, when it comes to dropdowns, only the values are captured and not the text of the selected option. <select name='attend'> <option value="1" selected="selected">Question&l ...

Exploring Joins in Laravel 4 Query Builder

In my database, I have tables named "airport" and "route". The "id" of "airport" is a foreign key in "route" (i.e. Origin, Destination). Airport +-------+-------------+-----------------------+ | id | airportcode | Location | +-------+--- ...

The most straightforward way to understand $query->row() and $query->result() functions

Lately, I've been quite frustrated with the behavior of CI. It could be because I tend to overlook small details at times. Anyway, onto the question! I'm currently working on an application where I need to retrieve a single row using active recor ...

Instructions on merging the firstName and lastName fields to display them in the Full name column within Sonata admin

I am attempting to display a column for Full name that combines the firstName and lastName properties from my entity. How should I go about this? Below is the Entity and Admin.php code snippets: class test{ private firstName; //other properties privat ...

Shopping cart feature in PHP - Ajax response only functioning once

I'm in the process of developing a shopping cart. I start by adding products to the cart, then use Ajax to dynamically update the quantity when it changes. Everything works smoothly after the first change, but subsequent quantity updates do not refres ...

Using Ajax and PHP to Trigger a Forced Download

I am trying to develop a download script that enables the Force Download of JPGs. Below is my PHP script: <?php header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); ...

Is there a way to execute an SQL query using ajax?

I recently came across an interesting problem involving passing an SQL query through to an ajax file. let qry = mysqli_query($this->con,"SELECT * FROM products"); To ensure proper containment, I embedded the variable within HTML using the following co ...

Frontend experiencing issues with Laravel Echo Listener functionality

I have recently developed a new event: <?php namespace App\Events; use Illuminate\Broadcasting\Channel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\PresenceChannel; use Illuminate&bs ...

What is the best way to verify HTML using RSS?

Looking to improve my skills in HTML/CSS/PHP development, I'm faced with a challenge when it comes to validating code that contains content beyond my control, such as an RSS feed. For instance, on my home page, which is a .php document containing bot ...

Unable to retrieve data from PHP array field

I have a function that returns an array containing 3 objects, and I want to access the data of the third object from this array. After retrieving the array, I can use var_dump to see the entire array, as well as to view a specific object within the array. ...

No default value error occurs when using the set mutator and the update observer is not functioning properly

When I try to save a model, I encounter an error stating that the slug attribute does not have a default value. To resolve this issue, I created a mutator called setSlugAttribute. However, it still gives me the same error. //Inside the Controller's sa ...

Local Wordpress pages have ceased to function on my computer

While managing my WordPress website, I had previously installed a quick cache plugin on my XAMPP server localhost. However, all of a sudden, the pages stopped working. The site is currently not hosted online but running on my local machine. Upon inspectin ...

Transmitting JSON data using a Jquery AJAX request in PHP

I'm facing an issue passing a JSON array from a jQuery AJAX call to a PHP file and attempting to write the received data to a file. This is my code: var contacts = [{"address":[],"phone":[],"last_name":"abc","email":[{"address":"<a href="/cdn-cgi/ ...

Using PHP to extract information from a CSV file

I am dealing with a CSV file titled mail.csv that contains the following information: d,2012-08-24 00:00:57+0200,2012-08-24 00:00:45+0200,<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5dbdac7d0c5d9ccf5dbd0c2c69bd6d4d8d0c7d ...

Encountered a MYSQL error while attempting to update the record: Unable to access table (Temporary table)

Just started learning MYSQL and PHP last week, so bear with me if you come across some questionable coding in the script below. Here's my issue. When I run this PHP script: if($hdd!==0){ $sql="CREATE TEMPORARY TABLE custom AS SELECT * from `builds`; ...