htaccess - Redirect one folder to another internally - .htaccess

I would like to redirect all no-files and no-directories of one folder to another. The .htaccess should be on root (http://example.com).
Example:
http://example.com/admin/core/file.php - Does not exists. So, redirect (internally) to http://example.com/adminhide/core/file.php
http://example.com/admin/index.php - Exists. So, don't do anything..
I tried to create the code, but I'm having an 500 Internal Server Error:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/admin/$
RewriteRule (.*)$ /adminhide/$1 [L]

Related

Use .htaccess file to check folder for file and redirect if not found

I want to use .htaccess files so that if someone visits an old URL on our new website such as website.com/oldpage.asp .htaccess first checks to see if website.com/oldsite/oldpage.asp is on our server. And if no file is there redirect them to old.website.com/oldpage.asp where a copy of our old website exists.
For some reason only the .htaccess file in the /oldsite/ folder is executing.
Here is my .htaccess file for my site root which checks if the file exists on the new site and if not redirects to /oldsite/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /oldsite/$1 [NC,QSA,L]
Then in /oldsite/ its supposed to check if the file again exists, and if not redirect again.
RewriteEngine On
RewriteOptions inherit
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://old.website.com/$1 [R=301,NC,QSA,L]
You don't need 2 .htaccess for this.
Have this rule in site root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/oldsite/$1 -f
RewriteRule ^(.*)$ oldsite/$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://old.website.com/$1 [R=301,L]
Then remove /oldsite/.htaccess and test this after clearing your browser cache.

Rewrite cond - What is actually doing?

I have a WP instance, and I see that any time I write a URL that has any string that contains 'moz' it errors out with a 404.
I have taken a look at my .htaccess looking for 'moz' and I see this:
RewriteCond %{REQUEST_URI} !^/moz(.*)
Why would that be causing a 404?
EDIT: The context of this RewriteCond
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/knowledge/
RewriteCond %{REQUEST_URI} !^/midphase(.*)
RewriteCond %{REQUEST_URI} !^/moz(.*)
RewriteCond %{REQUEST_URI} !^/Stats/(.*)
RewriteRule . /index.php [L]
It looks like someone added WordPress to a website with existing content. Those rewrite conditions check to see if the URL is for one of the existing folders, and if not, send the request to index.php.
That means of you enter a URL that starts with moz and points to a file that doesn't exist, you'll get a 404.

Redirect file type specific requests

In my app, I have the srcs of many images like this:
path/other-path/my-image.jpg
This can get long and can expose some of my file structure to the outside world.
What can I do in .htaccess to redirect a given file extension to a directory?
i.e. my image src is simply "my-image.jpg", .htaccess sees that is a .jpg file and redirects to the folder "www.site.com/my-jpgs/" were "my-image.jpg" resides, thus loading the image.
Here is what I have at the moment:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/importers
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$
RewriteRule ^(.*)$ public_html/index.php
RewriteBase /
# Ignore rule if the real path exists, use that instead.
RewriteCond %{REQUEST_FILENAME} !-f
# Only use the filename and extension.
RewriteRule (.[^/]*)\.(gif|jpg|png) public_html/images$1.$2 [R,L]
I've sort of got it working, except the browser makes two requests that look like this:
Request URL:http://www.view.com/activity/enquiry/dropdown-btn.gif
Request URL:http://www.view.com/public_html/images/dropdown-btn.gif
The second being correct, how do I correct this so it doesn't make the first incorrect request?
Ordering of rewrite rules is very important in .htaccess. Your problem is incorrect ordering of your rule. You need to move last rule to the top just belowRewriteEngine Online to have external redirect before sending everything off toindex.php`:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteBase /
# Ignore rule if the real path exists, use that instead.
RewriteCond %{REQUEST_FILENAME} !-f
# Only use the filename and extension.
RewriteRule ^(?:[^/]+/)*([^.]+\.(?:gif|jpe?g|png))$ images/$1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/importers
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$
RewriteRule ^ index.php [L]

.htaccess redirect non-existing pages only, keeping the token

I want to redirect
www.domain.com/abc
to
www.domain.com/index.php?a=abc
only if the folder abc doesn't already exist.
How should I write the .htaccess file?
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ http://www.domain.com/index.php?a=$1 [L]

.htaccess redirection to folders and subfolders

I am having issues with htaccess redirections. Following is the issues I am having.
The root folder to which the main domain actually points has the following folder: "example"
example.com is actually internally linked to the path "root/example/" rather than "root/". This is done using the following htaccess code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/example/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ example/index.html [L]
The folder "exmaple" has the following subfolders: newexample, newexample_beta
If I go to the URL "example.com/newexample", the URL needs to be internally linked to "root/example/newexample". But the URL redirects me to "example.com/example/newexample/"
could you please let me know what needs to be done

Resources