Yogesh Chauhan's Blog

How to Skip or Exclude a Specific URL or File From .htaccess Rewrite Rule?

in PHP on November 21, 2019

In this blog post, we saw how to remove the file extension from URL.

Now, let's see how to keep extension for some files as some of they might be getting data from _POST methods and sometimes errors can occur while redirecting those pages.

To remove the extension we saw the following code in this blog post.

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /.*index HTTP/
RewriteRule ^(.*)index$ http://your-domain-name.com/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://your-domain-name.com/$1 [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /(.+).php HTTP/
RewriteRule ^(.+).php$ http://your-domain-name.com/$1 [L,R=301]
RewriteRule ^([a-z]+)$ /$1.php [L]

The code above will remove all the PHP file extensions from your website URLs. 

Now, let's assume you have one URL named "get-user-data.php" and you want to skip that file from removing the extension. Just add the following line to your htaccess code.

RewriteCond %{REQUEST_URI} !^/get-user-data.php$ 

Just add that line into the code above and change the name and extension according to your file name and extension and you're good to go.

Also, I know I have already discussed this in this blog post, but don't forget to add the following code to your htaccess file OR your server will have problems allowing files without extensions. 

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
What is Hadoop and Hadoop Ecosystem?MiscellaneousHow to draw an SVG path on scroll using JavaScript?JavaScriptHow to implement NSNumberFormatter in Swift for currency?SwiftHow to insert Bootstrap 4 in Angular 9 app?AngularHow to load a module with configuration in SCSS?SCSSAt-rules: @error and @warn in SCSSSCSS