Redirecting htaccess based on folders and file extensions with capitalization variations

Transitioning content from an old Windows site to a new CMS setup presents some challenges, particularly when it comes to preserving certain pdfs/images that are linked from external sources. To maintain these links without redirection, some folders and files from the old setup must remain intact.

The difference in capitalization between Windows and Linux poses another obstacle, especially when root folder names clash with new permalinks on the website. Specific rewrites are needed to address these issues while also accounting for potential misspellings or capitalization errors.

For instance, there may be a folder named Industries (with a capital 'I') on the old system and a 'industries' permalink (with a lowercase 'i') on the new site. In this case, submitting the exact URL will retrieve the correct content - site.com/industries for the webpage and site.com/Industries/whatever.pdf for the file within the leftover Industries folder.

The challenge arises when inbound links include variations like site.com/industries/whatever.pdf (no capitalization) or site.com/INDUSTRIES/whatever.pdf (all caps), resulting in 404 errors in the CMS.

To address this issue, a targeted rewrite rule is required specifically for pdf, jpg, and png files accessed through specific folders. It should accommodate common spelling variations to avoid 404 errors and gracefully handle cases where no match is found, allowing processing by the CMS to proceed uninterrupted.

In attempting to create such a rewrite rule for the example of the existing folder "Industrial," here is my current progress:

  RewriteCond %{REQUEST_URI} ^(.*)industrial(.*(jpg|png|pdf))$ OR
  RewriteCond %{REQUEST_URI} ^(.*)INDUSTRIAL(.*(jpg|png|pdf))$
  RewriteRule ^(.*)$ /$1?%1Industrial%2 [L,R=301]

I am still working on refining this approach to achieve the desired outcome. Any suggestions or guidance would be greatly appreciated. Thank you.

Answer №1

Here are the rules for your root .htaccess file:

RewriteCond %{REQUEST_URI} ^/industries/(.+?)\.(jpg|png|pdf)$ [NC]
RewriteRule !^Industries/ /Industries/%1.%2 [L,R=301,NE]

Answer №2

To ensure that the case is not a concern, simply utilize the [NC] flag for case insensitivity. This way, the format in which the data arrives won't pose an issue. The snippet of code below demonstrates this concept. For example, if the URI appears as

http://example.com/Industrial/someimage.jpg

it will still be recognized. Even if the characters are in a different case, the NC flag allows for a successful match without needing to perform any rewrites.

RewriteCond %{REQUEST_URI} ^/industrial/(.+)\.(jpe?g|png|pdf)$ [NC]
RewriteRule ^ - [L,NC]

Visit this link for more information on the [NC] flag

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

Disabling Bootstrap Js, JQuery, and CSS in Yii2

Can anyone suggest an alternative to using bootstrap.css and bootstrap.js? I've attempted the following: 'assetManager' => [ 'bundles' => [ 'yii\bootstrap\BootstrapAsset' => [ & ...

Guide on dynamically loading a PHP file into a div using jQuery Load method and passing parameters

I have an element <div id="search_result"></div>, and I used $.ajax to fetch some data (search result). $.ajax({ url: "url to server", dataType: "json", data: keyword, type: "post", success: function(data){ /* load searchResult.p ...

Can you explain the distinction between using echo with curly braces and without, and the reasoning behind having both options available?

Many of us who program in PHP have been taught to use echo "string";. It's a common practice, but have you ever wondered why we use it differently than other functions? So, the question is: echo "Some String"; Why do we do this instead of echo("So ...

Storing Form Data in MySQL, upon clicking the submit button the user is redirected to a PHP page and the information is not

Currently, I am facing an issue with injecting data into my SQL table. Whenever I input information into the forms and click on the submit button, the page redirects me to my PHP file without actually injecting any data. The tables remain empty. Below is ...

Symfony allows for unique field validation for one-to-many relationships

My Request entity contains multiple Interventions structured as follows: Request.php /** * @ORM\OneToMany(targetEntity=Intervention::class, mappedBy="request") * @Assert\Count(min=1, max=3) * @Assert\Valid ...

Update MYSQL table values using AJAX and jQuery, then dynamically refresh the updated values on the web page

Hey there! I am fairly new to utilizing ajax and encountering some difficulty with a fundamental concept. In my MySQL table called "users," I have various user information stored, including the balance they pledge to donate. My goal is to create a div elem ...

Amazon S3 Landing Page Featuring Contact Form

Although S3 is not a fileserver, it serves as an excellent tool for managing static websites. The majority of my projects are 99% static, making it ideal for this particular project. As an AWS Solutions Architect, I am struggling to find the most straightf ...

What could be the reason for my cron job functioning in the command line but not in PLESK?

I'm facing a problem with setting up a cronjob that was working seamlessly on a PLESK 9 dedicated server, but for some reason, it's not functioning on my new PLESK 10 dedicated server. I can't seem to figure out the root cause of this issue. ...

Submitting a page with PHP, Ajax, and JSON

Need help with making an Employee search functionality in my business using Ajax. After submitting the form, I want to load employee details using jQuery-based Ajax. Here is the code for searching employees. The problem I'm facing is that after submi ...

What is the best way to handle identical routes within two separate route groups in Blade?

I have set up two resource controllers within separate route groups - one for users and one for admins. Route::group([ 'prefix' => 'dashboard', "middleware" => 'auth', "namespace" => 'U ...

When the HTML and PHP code keeps running, the JavaScript text on the page updates itself

I was experimenting with threading in different languages like Java, PHP, and JavaScript. I am aware that JavaScript is single-threaded and PHP lacks robust threading capabilities. Below is the code snippet I have been working on: <body> <p id= ...

What steps can be taken in PHP to prevent a user from logging in more than once with the same account?

My PHP application uses sessions, and I want to prevent users from sharing their credentials with third parties who could access the account simultaneously. Is there a method to ensure that when a user logs in, it will kick out any previous sessions? Add ...

Utilizing the Yoast Wordpress SEO API to Set a Unique SEO Title

After installing a new plugin on my website, I've encountered an issue with my custom titles no longer working as intended. For more information, you can refer to the Wordpress SEO API documentation here: [http://yoast.com/wordpress/seo/api-docs/] In ...

What are alternative methods for reading JSON files in PHP that do not involve file_get_contents()?

Recently, I developed a basic PHP script that fetches data from a local JSON file and uses it to respond to various queries directed at the page. In my implementation, I adopted the usage of file_get_contents() for reading the file. However, as the number ...

Is there a way I can obtain the code for a message box?

When I refer to a message box, I am talking about a container that gives users the ability to input their text and access different features like BOLD, ITALIC, color, justify, etc., in order to customize their message's appearance! (Think of it as the ...

Using Phonegap for making AJAX requests

Working with Android 2.2, I have my application integrated with phonegap and Sencha. The forms in the app utilize Ajax requests to submit information via email. Everything functions smoothly until the application is wrapped in Phonegap. I have learned th ...

Using JQuery to toggle a fixed div at the bottom causes all other divs to shift upwards

I'm currently working on a chat feature using node JS, and while the functionality is perfect, I've run into an issue with the CSS aspect of it. The problem arises when we have multiple tabs and clicking on just one causes all the tabs to move u ...

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

Group by a specific custom field

Currently, I am utilizing Wordpress for the purpose of generating and filling a <select> with various <option>s. <?php if( $lh_loop->have_posts() ): ?> <select class="mousechoice left"> <?php while( $lh_loop->have_p ...

Guide on utilizing PHP to display a particular value from a JSON file

Hello, I am trying to extract specific data from a JSON response. Here is the structure of the JSON: { "data":{ "user_quota":[ { "group_limit":10240, "quota":0, "support_share_quota":false, ...