htaccess autodetect absolute path - .htaccess

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?

Related

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.

modifying part of URL using htaccess

I've moved my abc folder from /myproduct/abc directory to /myproduct/extensions/abc on my server.
How can I redirect all calls to http://localhost/myproduct/abc to http://localhost/myproduct/extensions/abc ?
Ex : if requested URL is http://localhost/myproduct/abc/pqr.php, it should be redirected to http://localhost/myproduct/extensions/abc/pqr.php
Basically I want .htaccess code that can be placed inside /myproduct folder and if someone requests URL like http://localhost/myproduct/abc/pqr.php then it will look for occurrence of /abc/ and replace it with /extensions/abc/
We cannot replace /myproduct/abc/ by /myproduct/extensions/abc/ as myproduct can have white-labelled to yourproduct or myproduct1 etc..
Any help would be highly appreciated. :)
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/abc/([^\.]+)\.php/? [NC]
RewriteRule .* %1/extensions/abc/%2.php [R=301,L]
Redirects
http://localhost/Any/Number/Of/Folders/abc/AnyFileName.php
To:
http://localhost/Any/Number/Of/Folders/extensions/abc/AnyFileName.php
To keep the first URL showing in the browser's addres bar, remove R=301 from [R=301,L]
NOTES:
If only folder myproduct/ is needed, like this: http://localhost/myproduct/abc/AnyFileName.php The rule will work too.
If file AnyFileName.php, pqr.php for example, exists at folder abc in the incoming URL, the rule will be skipped (Makes no sense to have it there anyway). The script has to be at folder abc in substitution URL: http://localhost/Any/Number/Of/Folders/extensions/abc/.
Vijay,
Try this:
RewriteEngine On
RewriteBase /
# REQUEST_FILENAME should not be file name
RewriteCond %{REQUEST_FILENAME} !-f
# REQUEST_FILENAME should not be directory name
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/abc/(.*)/? [NC]
RewriteRule .* %1/extensions/abc/%2 [R=301,L]
Which server are you using? Have a look at mod_rewrite in case you are using Apache 2.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

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

htaccess rules losted when directory exists

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.

Check for a directory inside a subdirectory dynamically

I have a bunch of domains which points to the same directory "public_html" on my host.
There is a .htaccess file in the main folder that locally redirects each domain to a particular folder with the same name as %{HTTP_HOST} mod_rewrite variable. (eg: redirect www.domain.com to public_html/www.domain.com/)
This is the content of the .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond /%{HTTP_HOST}/#%{REQUEST_URI} !^([^#]+)#\1
RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [L]
I'm having a problem when it comes to the Directory Slash directive.
If I try to access a folder in the domain url without the forward slash like http://www.domain.com/folder, the mod_dir applies the DirectorySlash and externally redirects my request to http://www.domain.com/www.domain.com/folder/
I tried applying a 301 redirect AFTER the domain directory redirect like this:
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]
But for this to work, I would have to be able to check if the %{REQUEST_FILENAME} exists inside the %{HTTP_HOST} 'folder'. I even tried the following, for no success:
RewriteCond /%{HTTP_HOST}/%{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]
How can I check, Dynamically, if the %{REQUEST_FILENAME} is a directory, which would be inside a directory with the same name as %{HTTP_HOST}?
Thanks in advance
If you work with -d you need to provide an absolute filesystem path. So try this:
RewriteCond %{DOCUMENT_ROOT}/%{HTTP_HOST}%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]

Resources