.htaccess redirect from /subfolder/file to root - .htaccess

I need to redirect every URL that requests a file in /subfolder, for example: example.com/subfolder/food/apple.pdf -> example.com
I tried creating a .htaccess inside /subfolder that contains:
RewriteEngine on
RewriteRule ^subfolder/.*$ / [R=301,L]
Unfortunately, without success. Is there a way to achieve this?
apple.pdf and all files inside the /subfolder do not exist anymore (that's why I need a redirect)

Related

Use .htaccess to redirect root to two different directories

I'm developing a website that has to co-exist with another one on an existing server. I know it's not an ideal situation but for timing reasons we don't really have any choice.
Request coming to the root of the URL have to be routed to the index.html of the new website, and everything else has to go the index.html of the old website (both of the websites are using react so every route goes to index.html).
I've tried to write a .htaccess doing this but with no success so far.
Here's an example of how it's supposed to work :
I have two folders, old/ and new/ each containing a website.
if a user goes to http://example/ -> it's serving content from the new/ folder.
if a user goes to http://example/test -> it's serving content from the old/ folder.
Below is what I've tried. It redirects / to the new/ folder, but rewrites the URL doing so. Furthermore, as soon as I try to redirect /* to the old/ folder things stop working.
# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
# Redirect requests to index.php
RewriteCond %{REQUEST_URI} ^$
RewriteRule ^$ new [L]
Try the following instead:
Options +FollowSymLinks
RewriteEngine On
# Rewrite the root only to new/index.html
RewriteRule ^$ new/index.html [L]
# Rewrite other URLs to old/index.html
RewriteRule . old/index.html [L]
You should rewrite directly to the file that is handling the request. If you rewrite to new only - without a trailing slash - then mod_dir will issue an external redirect to append the slash.
NB: This does assume that you have another .htaccess file in the /old/ subdirectory that contains mod_rewrite directives (a front-controller), otherwise everything (including static resources) will be rewritten to /old/index.html.
It redirects / to the new/ folder, but rewrites the URL doing so
I think you are using the terms "redirect" and "rewrite" the wrong way round. A "rewrite" is internal to the server, the URL does not change. This is what you are doing here... rewriting from root to /new/index.html etc.
However, a "redirect" is an external HTTP redirect that triggers a 3xx response and a second request to the server.

How to redirect my sudomains webspace to the actual subdomain?

I want to redirect https://example.com/sub/index.php to https://sub.example.com/index.php.
As you can tell, the subdomains webspace is in the actual domain webspace; this leads to duplicate content.
I already tried this:
Adding javascript to index.php's head:
<script type='text/javascript'>document.location.href='https://example.com/sub/index.php';</script>
But this creates an infinte loop.
Adding .htaccess to /sub/ directory:
RewriteEngine On
RewriteRule (.*) https://sub.example.com/index.php$1 [R=301,L]
But this makes it impossible to create other files in /sub/ because it always redirects you to index.php
What would be the best way to solve this if I also want to create other files in /sub/ which always redirect to the subdomain version?
The following placed at the root of your site should redirect anything that hits example.com/sub to sub.example.com.
RewriteEngine On
RewriteRule ^sub/(.*)$ http://sub.example.com/$1 [R=301,NC,L]
You can test it at https://htaccess.madewithlove.be/ if you need to tweak those rules.

redirect /subdirectory to /symlink/subdirectory where /symlink points to /

i'm working on a drupal7 multisite setup based on subdirectories where example.com is used for the main website and example.com/subsite is another standalone drupal install. subsite is a symlink located in the root directory and also pointing to the root directory to give the subsite access to drupal core files.
now i have to make static content available via example.com/subsite/static, so i created a directory static in the root directory. that all works fine.
the problem is, that example.com/static is now also accessable and i want to prevent that.
i tried to redirect all requests to /static to /subsite/static resulting in inconsistent behaviour and redirect loops.
directory structure:
/
/{various drupal directories}
/subsite -> /
/static
rewrite rule:
RewriteRule ^/static/(.*)$ /subsite/static/$1 [R,L]
thx in advance
Is this what you want?
RewriteBase /
RewriteRule ^static/(.*)$ /subsite/static/$1 [L,R=301]
RewriteRule ^subsite/static/(.*)$ /static/$1 [L]
In the first line we redirect all call to /static/ to /subsite/static/.
In the second line we rewrite all call to /subsite/static/ to /static.
If it doesn't work please post your whole .htaccess file.
i fixed the redirect loops by using a rewrite condition, should have thought of that before.
RewriteCond %{REQUEST_URI} !^/subsite/(.*)$
RewriteRule ^static/(.*)$ /subsite/static/$1 [L,R=301]
thx anyway #florian-lemaitre for trying to help me

.htaccess rewrite from subdirectory

Any help with this would really be appreciated.
I am setting up 301 redirects in a .htaccess file to redirect hundreds of old urls to the latest live pages.
My .htaccess file is currently located in the root of the domain and looks something like this:
RewriteCond %{QUERY_STRING} ^pName=product-one$ [OR]
RewriteCond %{QUERY_STRING} ^pName=product-two$ [OR]
RewriteCond %{QUERY_STRING} ^pName=completely-different-name$
RewriteRule ^catalog/product_info.php$ http://www.mydomain.com/new-product-name-p-123.html? [R=301,L]
While researching for answers on how to bulk redirect to one url using .htaccess, I read that it would wise to change the location of the .htaccess file and place it in the specific directories to which it apply's, so requests to other directories won't even these rules.
So I have placed the .htaccess file in the specific sub directory, which is catalog, but the redirect no longer works. What am I missing here? I want the rules for the urls to be redirected to be placed in the .htaccess file inside the catalog folder so all those rewrite rules wont be loaded each time the root .htaccess is loaded.
I would suggest you change your redirect 301 line to contain the path directly to you catalog folder of OSCommerce.
So instead of using
RewriteRule ^catalog/product_info.php$ http://www.mydomain.com/new-product-name-p-123.html? [R=301,L]
use e.g.:
Redirect 301 /website/catalog http://www.mydomain.com/page2.htm
or
Redirect 301 /catalog http://www.mydomain.com/page2.htm
Option 1) is if your catalog is located at http://youdDomain.com/website/catalog/ .
If it is located at http://youdDomain.com/website/aaa/catalog/ you would use:
Redirect 301 /website/aaa/catalog http://www.mydomain.com/page2.htm
I tested this in my own webserver - hope it helps.
When you put the .htaccess in a subdirectory, you must adjust the RewriteRule pattern accordingly. mod_rewrite strips the directory prefix, where the .htaccess file is located, see RewriteRule - What is matched?.
You must change the rule to
RewriteRule ^product_info.php$ ...
If you have lots of URL paths to rewrite, you might also want to look into RewriteMap.

How to redirect any folders within a subdirectory to a specific file using htaccess?

I want to redirect any folder to a specific php file with the folder name as variable using htaccess, for example:
www.domain.com/subdirectory/folder1 redirects to www.domain.com/subdirectory/file.php?id=folder1
www.domain.com/subdirectory/folder2 redirects to www.domain.com/subdirectory/file.php?id=folder2
This should work if the folders have "/" at their end, for example,
www.domain.com/subdirectory/folder3/ redirects to www.domain.com/subdirectory/file.php?id=folder3
It will be better if I can put the .htaccess file in the subdirectory and get redirect rule according to it.
I created a .htaccess file with the following code while putting the file in the subdirectory:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)$ file.php?id=$1
but it does not work.
RewriteRule ^folder(.*)$ file.php?id=$1
use these i am not tested but it will be work.

Resources