.htaccess redirect all images in image folder - .htaccess

Ok, so I have a small predicament. I have a large image gallery that has over 30,000 images inside of it. I've decided to change to a newer piece of software, and this new gallery is completely different in the way image URLs are handled. So a simple redirect isn't going to cut it anymore (there is no way to determine a connection between A and B, per se, because the old gallery used ID's whereas the new one uses titles for SEO).
However, I would like to still accomodate to the old URL's by redirecting visitors to the corresponding image files. The script that they were contained within maybe gone, but the database and ID's of each image are still there - I was thinking to just rename every image file to the match the ID in the database, and use a redirect rule somehow to link to them.
The old URL structure looked something like this:
domain.com/image/3343/
All of the images are contained within the /images/ folder. If I rename every image filename to match the ID's (as above), how can I use htaccess to redirect the URLs to these files?
Keep in mind.. I need a solution that doesn't involve 30,000 rows in a htaccess file :)

Try:
RedirectMatch 301 ^/image/([0-9]+)/$ /image$1.jpg
Or using mod_rewrite:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^image/([0-9]+)/$ /image/$1.jpg [L,R=301]
For inside the image folder:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/$ /image/$1.jpg [L,R=301]

Related

How to redirect HTML index file to subfolder directory?

I have created a subfolder to access my HTML website. But the problem is, it is not running on the subdirectory URL but with the index.html aliases.
e.g. https://example.com/my-html-site/index.html (the website running the whole page).
But I want a simple redirection to run the website under the subdirectory like this below,
https://example.com/my-html-site/ (when I hit this URL then it shows a blank page).
Is there any way to redirect the https://example.com/my-html-site/index.html to https://example.com/my-html-site/ via htaccess file rules?
Thanks in advance,
Based on your shown samples could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ $1/index.html [L]

.htaccess sub-folders to read parent folder file while maintaining 'pretty' url

This will have been asked and will have an answer. I cannot remember what this feature is and attempts to once again learn it have come up dry.
I'm looking to place a .htaccess file within a folder where the file to read is located, anything beyond that will read that file.
Example:
www.url.com/articles/animals
www.url.com/articles/animals/safari
www.url.com/articles/animals/safari/lions...
Would display the file at
www.url.com/articles/file-to-read.php
So you can then grab the animals, safari, lions to display the necessary content*, maintaining the 'pretty' URL.
*To clarify, I'm not asking for the code to obtain the path parts, this is just an example usage of said parts.
I think you are talking about the pattern matching and rewriting to the actual file on server, something like below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^articles/(.*)$ articles/$1.php [L]
Or
RewriteRule ^articles/(.*)$ articles/filetoread.php [L]

htaccess rewrite url, add dir and remove query

I have two versions of images on my server, one with and one without a watermark. On my one website I want to have to non-watermarked version. On the websites that hotlink my images I want to have the ones with the watermark.
I want to rewrite the url to serve the watermarked images on those website. The url structure on my website is this:
www.example.com/wp-content/uploads/image-name.jpg?t=1512472796
and I want to change it to:
www.example.com/wp-content/uploads/wm/image-name.jpg
I am kind of stuck here, no idea to do the part that adds the wm dir to the url and removes the query:
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
RewriteCond %{QUERY_STRING} ^t=([0-9]*)$
Any help is highly appreciated.

.htaccess cascading conditions

I'm trying to figure out how to manage potentially conflicting conditions in .htaccess
My setup is the following:
- I have a CMS running on a server that can be accessed through myCMSdomain.com where myCMSdomain.com would be CMS home page and myCMSdomain.com/admin would be the admin interface.
- Sites using this CMS should be pointing to myCMSdomain.com/sites/index.php
- Images for all sites are available somewhere behind myCMSdomain.com/admin/images/sitename/...
So here is how I tried to tackle this problem:
RewriteCond %{HTTP_HOST} !^(www.)?myCMSdomain.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sites/index.php
With this, all incoming requests coming from other domain names are treated correctly by the index.php file but as images are hosted somewhere else, I'd like to use a rule saying that images should be fetched somewhere else like this:
Visible url format for images is: images-[sitename]/[image_path]
Real location of the images is: admin/site/[sitename]/[image_path]
The following rule works but not in combination with the first rule
RewriteRule images-([a-zA-Z0-9]+)/(.*)$ admin/site/$1/images/$2
Images end up calling index.php instead of using the rule I defined for them.
I have tried to excluse the image directory from the conditions but it doesn't work either:
RewriteCond %{REQUEST_URI} !(images-([a-zA-Z0-9]+)/(.*))
I might have similar issues in the future with other exception so I was wondering if there was a way to handle this.
Thanks!
Laurent
UPDATE 1:
If I use the following rule on top of all other
it works only if I'm using myCMSdomain.com domain name
if I use any other domain like anotherdomain.com, the rule leads to a http 500
RewriteRule images-([a-zA-Z0-9]+)/(.*)$ manager/site/$1/images/$2
So http://www.myCMSdomain.com/images-test/test.jpg leads me to the correct image
But http://www.anotherdomain.com/images-test/test.jpg leads me to a 500 http error code while this domain is pointing correctly to sites/index.php
UPDATE 2:
On Justin's request, here is a view on the physical directory structure on the server
/admin/
/admin/site
/admin/site/site_name/
/admin/site/site_name/images/
/sites/
/sites/js
/sites/css
You can rearrange your conditions logic.
# if www.myCMSdomain.com or myCMSdomain.com -> do nothing
RewriteCond %{HTTP_HOST} ^(?:www\.)?myCMSdomain\.com$ [NC]
RewriteRule ^ - [L]
# if we reach here, this means it's a subdomain/another domain
# images rule
RewriteRule ^images-([^/]+)/(.+)$ /admin/site/$1/images/$2 [L]
# not a file/directory -> sites/index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /sites/index.php [L]
It is also possible the way you did but it would be longer to write.
Also, keep in mind that conditions (RewriteCond) are non-cumulative: they are for next rule (RewriteRule) only.
I've been struggling with this issue for some time now but at last I think I have found the issue. It looks like there was something wrong in the domain name configuration, I don't know exactly what but once I had re-saved the mapping of all domains, the htaccess worked the way it should.
In the end I have used Justin's proposition, it looks more future proof than mine.
Thanks for your help and happy new year to all
Laurent

htaccess seo friendly urls for all subfolders

i have script in index.php in ALL folders
need to make pretty url/seo friendly urls like this: site.com/folder1/something.html
now i have ugly urls like this: site.com/folder1/index.php?category=something
this part index.php?category=something is always the same in all subfolders, i just want to change it to more seo friendly like something.html
once again: find index.php?category=1$ and replace it with 1$.html
do not touch anything else, just this part of url
so when i visit:
site.com/another-subfolder/and-one-more-folder-here/index.php?category=something
need to see this in address bar:
site.com/another-subfolder/and-one-more-folder-here/something.html
i hope you get it?
i tried this with folder1 in htaccess in root
RewriteRule ^folder1/(.*).html$ folder1/index.php?category=$1 [L,R=301]
ok this works, but how can i make this to work for all subfolders accross the site like this:
site.com/folder145/index.php?category=something
site.com/subfolder/index.php?category=something
site.com/another-subfolder/and-one-more-folder-here/index.php?category=something
there will be 1000s of subfolders with different names so manually making RewriteRule for each subfolder won't work
any help is appreciated
First, you need to change all the links that you generate from the index.php?category= form to the category.html form.
Then in the htaccess file in your document root, you add these rules to internally rewrite the category.html form back to the index.php?category= form:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?/)?([^/]+)\.html$ /$1index.php?category=$2 [L]
Then, to point links you don't have any control over (or ones generated using a FORM) externally redirect any direct access to links in the index.php?category= form:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ (/.*?)?/index\.php\?category=([^&\ ]+)
RewriteRule ^(.+?/)?index\.php$ /$1%3.html? [L,R=301]
This should match any and every subdirectory.

Resources