htaccess rules losted when directory exists - .htaccess

this is my code at .HTACCESS file in my web server:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This code works well, but when a directory exists the website redirect to Index of (Apache directory browsing):
For example:
http://www.mywebsite.com/XXXX This redirects to the URL http://www.mywebsite.com/XXXX and the page show information about XXXX. (Directory XXXX doesn't exists)
Directory ZZZZ exists and http://www.mywebsite.com/ZZZZ redirects to Index of (Apache directory browsing)
Finally, the question is how to redirect as 1. example.

RewriteCond %{REQUEST_FILENAME} !-d
This line disables rewriting for existing directories. Simply remove it if you always want to rewrite.

Related

.htaccess Rewrite Rule Understanding Problems

Site Structure
/articles/Employment/Companies.php
/articles/Employment/Companies/.htaccess
/articles/Employment/Companies/index.php
.htaccess file reads
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ index.php [L]
So when you go to
/articles/Employment/Companies/[company type]
It is displaying the index.php page.
The Problem
I'm trying to link to
/articles/Employment/Companies.php
without the .php being displayed, however if I link to
/articles/Employment/Companies
it is going to
/articles/Employment/Companies/
What i'm Ideally Looking For
Understand why I my site is adding the / when linking to folder/hello
to strip out all .php so if you go to /hello it'll display /hello.php apart from in certain directories such as my current .htaccess file is located where /this or /that will display /index.php.
Please try with below, use from rewritecond with your existing rule what I am doing if the request is actually for php file which is not index.php then serve the extension less code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule !index.php$ $1.php [L]

htaccess autodetect absolute path

I am trying to make a 404 redirect in a subdirectory on my server. Ex https://website.com/subdirectory/ when a user enters an incorrect url.
Can I determine the absolute path to this subdirectory using htaccess?
I tried:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /server/404.shtml [R=301,L]
so my 404.shtml resides in public_html/website/subdirectory/server/ meaning it's not directly in the root directory.
Or is there something like RQUEST_URI (some sort of a reques_path) but show the path up to the filename?

htaccess not working in sub directory

My website is hosted in a sub directory (http://example.com/folder).
In root directory I forward http://example.com to http://example.com/folder where I have created a blog page and I rewrite URL like this:
RewriteEngine On
RewriteRule ^([^/]*)-(.+)\.html$ \/folder\/blog\.php?title=$1&bid=$2 [L]
This is working but it affecting other folder URL means like http://example.com/folder2 who will be redirected to my blog.
You can check if the directory or the file really exist and not apply the redirection with conditions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)-(.+)\.html$ \/folder\/blog\.php?title=$1&bid=$2 [L]

How to point all folders and files to root?

I want to point all subfolders and non-existent files to root folder.
www.domain.com/folder/david to www.domain.com/folder/
or
www.domain.com/folder/david/ to www.domain.com/folder/
You can try this in your /.htaccess file:
RewriteEngine on
# Make sure the requested path does not exist (file or folder):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If it doesn't exist, redirect it to `/folder/`
RewriteRule ^folder/(.*)/?$ /folder/? [NC,R,L]
The NC flag makes the rule case-insensitive. If you do not need it, you can remove it.
If you would like to make the redirect permanent, exchange the R flag for R=301.
If it doesn't matter whether or not it exists, use the following instead:
RewriteEngine on
# Redirect everything to `/folder/`
RewriteRule ^folder/(.*)/?$ /folder/? [NC,R,L]
Update
Based on your comments, it appears you are running an application in /folder/. As such, it would be better to place your .htaccess file in /folder/, and fill it with the following:
RewriteEngine on
RewriteBase /folder/
# Make sure the requested path does not exist (file or folder):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If it doesn't exist, rewrite it to index.php
RewriteRule ^ index.php [NC,L]
You then do not need an .htaccess file inside the root - only the one in /folder/ is needed.

.htaccess redirect if file or second level directory doesn't exist

I have a situation where I have a bunch of old content in a /products directory. Moving forward, new content is going in a /product folder. What I would like to do is redirect to /product if a file, or directory doesn't exist.
I have the following in a .htaccess file in the /products folder:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /product/$1 [R=301,L]
This works well for files in the /products directory and sub directories that contain index.htm files or other file names, however I'd like to limit a redirect on a non existing directory to be 2 levels:
/products/someproduct - Do not redirect
/products/someproduct/anotherproduct
If it doesn't exist, redirect to /product/someproduct/anotherproduct
How do I force the rule to only apply to sub directories that are 2 levels or more deep?
Try the following
RewriteEngine On
RewriteBase /
#for all /products/somefolder/any_folder_level_deeep content
RewriteCond %{REQUEST_URI} ^/products/([^/]+/.+)$ [NC]
#if file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
#and directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
#redirect to product folder
RewriteRule . /product/%1 [R=301,L]

Resources