I have a website with a public folder in which I host some images.
Let's say that the root folder is "public".
In this folder I have another folder called "images".
In the folder called "images" I have a lots of folders called "1"... "92345678".
I want to create a htaccess rule that will redirect the users from the folders:
www.website.com/public/images/
www.website.com/public/images/1
www.website.com/public/images/2
...
www.website.com/public/images/10000001
to
www.website.com/public/
Please give me an option that will work for all sub folders. (Recursive)
I also want to redirect only folders, the images urls should work!
You can use this rule in /public/images/.htaccess file:
RewriteEngine On
RewriteBase /public/images/
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . /public/ [L,R=302]
You must enable rewrite engine and then use rule to redirect from URI's that starting with /public/image to /public.
RewriteEngine on
RewriteRule ^/public/images.* /public
I found the answer:
#redirect all subfolders to root and exclude images
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^images/(.*)$ / [R=301,NC,L]
Thanks Mark Shevchenko and anubhava for your help!
Related
My domain is in my user folder. I would like to serve a file from another folder down a level, under the same user. I tried this:
RewriteRule ^test$ ../testfolder/test.txt [L]
But Apache serves a 400 Bad Request. Is htaccess capable of dropping down a level like this?
You may use this code to dynamically figure out PARENT directory and then rewrite to that:
RewriteEngine On
# dynamically figure out parent directory using composite RewriteCond
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)/[^/]+/\1$
RewriteRule .* - [E=PARENT:%2]
RewriteRule ^test$ %{ENV:PARENT}/test.txt [L,NC]
RegEx Demo
We have drupal 8 site with a folder in docroot. Lets say its in a folder called micrositefolder. It contains a single index.html file.
Now let's say micrositefolder lives on fullsite.com. I dont want someone to access the microsite via fullsite.com/micrositefolder, but instead only accessible via mymicrosite.com
I have already achieved that with the following:
# Prevent access to the static site from non-static site hosts.
RewriteCond %{REQUEST_URI} ^/micrositefolder [NC]
RewriteCond %{HTTP_HOST} !^mymiscrosite
RewriteRule .* /index.php [L,R=301]
# Only serve the static site if host begins with mymiscrosite.
RewriteCond %{HTTP_HOST} ^mymiscrosite
# Don't loop anything targeting the actual mask directory, to allow
# for linked scripts, stylesheets etc in the static HTML
RewriteCond %{REQUEST_URI} !^/micrositefolder/
#Any requests that made it this far are served from the /micrositefolder/ directory
RewriteRule ^(.*)$ /micrositefolder/$1 [PT]
That works great. I can now visit mymicrosite.com and it serves me that index.html in that folder.
I now have to include another page on that microsite. The url would be mymicrosite.com/ronnie. I created a folder inside of micrositefolder called ronnie with another index.html in it.
When I try to go to that url (mymicrosite.com/ronnie) it is being rewritten to mymicrosite.com/micrositefolder/ronnie/ and I cannot figure out why. I am pretty sure it has to do with that last line in my code snippet, but I cannot figure out how to make it just be mymicrosite.com/ronnie
One thing to note is if I view the url via mymicrosite.com/ronnie/ it works, but if I dont include the slash at the end it redirects to mymicrosite.com/micrositefolder/ronnie
You can add this rule below your existing rules in site root .htacess:
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L]
This will add a trailing slash if current request is pointing to a directory.
Problem with your proposed approach (in the answer) is that:
It will perform a trailing slash 301 redirect even if it is an invalid URI such as mymicrosite.com/qwerty111
For cases like mymicrosite.com/ronnie where /micrositefolder/ronnie is an actual directory, it will perform an extra 301 redirect before showing index.html
Adding an .htaccess inside the micrositefolder with the below seems to have solved my issue
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
</IfModule>
I need to use a .htaccess file to redirect all the pages, files and URL permutations within this IP address website, for example:
http://123.456.11.222/~account/downloads/customerxyz/oneofmyfiles.zip
http://123.456.11.222/~account/downloads/customer123/example
http://123.456.11.222/~account/downloads/newcustomer/test.jpg
...etc
to a new sub-domain like this
http://sub.newdomain.com/downloads/customerxyz/oneofmyfiles.zip
http://sub.newdomain.com/downloads/customer123/example
http://sub.newdomain.com/downloads/newcustomer/test.jpg
...etc
What is the most efficient way of doing this so all files and URL's in the downloads folder are redirected to the new location?
Thank you.
Create a /~account/downloads/.htaccess file with these rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^sub\. [NC]
RewriteRule (.+) http://sub.newdomain.com/downloads/$1 [L,R=302,NE]
lets say I have directory regions in my root:
root/regions
I want to load index.php file which is located in regions folder if user visits one of the following:
http://www.example.com/regions/united-kingdom
http://www.example.com/regions/united-kingdom/london
http://www.example.com/regions/united-kingdom/londom/rm8-16de
Country, city and post code dont exist as directory !
How to do that?
Thank you in advance!
In your regions/.htaccess you can have this .htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /regions/
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
The order should be DirectoryIndex index.html index.php // default is index.html.
If the webserver finds no files in the current directory that matches names in the DirectoryIndex directive, then a directory listing will be displayed to the browser, showing all files in the current directory. Apache will look for each of the above files, in order, and serve the first one it finds when a visitor requests just a directory.
Once check DirectoryIndex which is solution for you.
Let me know if you face any query/concern regarding this.
Thanks!
I moved wordpress files from root to a subdirectory. There are some files uploaded in wp-content/uploads folder. When accessing those files directly, I get a 404 error.
I need to rewrite the url only for the wp-content/uploads through .htaccess
What I need is to redirect from
http://example.com/wp-content/uploads/2012/09/report.pdf
to
http://example.com/wp_sub/wp-content/uploads/2012/09/report.pdf
This is what I've tried
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(.*)$ http://www.example.com/wp_sub/$1 [R=301,QSA,L]
This adds a subdirectory wp_sub to every link.
I need to add a subdirectory "wp_sub" only for wp-content/uploads links.
What am I missing?
Let's see what
RewriteRule ^(.*)$ http://www.example.com/wp_sub/$1
means:
You rewrite everything (^(.*)$) to http://www.example.com/wp_sub/$1.
Well, you don't want to rewrite everything, do you? :)
So we have to restrict this to only affect the wp-content folder:
RewriteRule ^(wp-content/.*)$ http://www.example.com/wp_sub/$1 [R=301,QSA,L]
Place this rule in your /wp-content/uploads/.htaccess file:
RewriteEngine On
RewriteBase /wp-content/uploads/
RewriteRule ^(.*)$ /wp_sub/$1 [L,R]
You can use the code :
RewriteRule ^wp-content/uploads/(.*)$ http://www.example.com/wp_sub/wp-content/uploads/$1 [R=301,L]