Redirect image URLs to new directories - .htaccess

I am migrating my site to a new software and need to redirect the old image URLs to new directory structures. This is the sort of pattern I need to redirect:
http://domain.com/galleries/directory1/*.gif > https://i.domain.com/galleries/1/big/*.gif
http://domain.com/galleries/directory2/*.gif > https://i.domain.com/galleries/2/big/*.gif
http://domain.com/galleries/directory3/*.gif > https://i.domain.com/galleries/3/big/*.gif
http://domain.com/galleries/directory4/*.gif > https://i.domain.com/galleries/4/big/*.gif
and so on
And also:
http://domain.com/upload/*year*/*month*/*day*/*.gif > https://i.domain.com/galleries/*year*/big/*.gif
Any help for what htaccess rules to use would be much appreciated. Thank you

The below should do the trick. However, be careful of collisions in file names from your uploads as the question doesn't specify different months and days in the target url.
RewriteEngine on
RewriteRule ^upload/([0-9]*)/[0-9]*/[0-9]*/(.*.gif)$ "https://i.domain.com/galleries/$1/big/$2"
RedirectMatch "^/galleries/directory(.)/(.*\.gif)$" "https://i.domain.com/galleries/$1/big/$2"

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(domain\.com)$ [NC]
RewriteRule ^(galleries)/directory(\d+)/(.+?\.gif)$ http://i.%1/$1/$2/big/$3 [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(domain\.com)$ [NC]
RewriteRule ^upload/(\d+)/\d+/\d+/(.+?\.gif)$ http://i.%1/galleries/$1/big/$2 [L,NC,R=301]

Related

.htaccess 301 redirect to new domain by editing path

I need to redirect old domain to new with .htaccess
Situation:
www.oldodmain.com/en/categoryA/product1
www.newdomain.com/en/categoryB
Result I am trying to achieve
www.newdomain.com/en/categoryB/categoryA/product1
Tried to do with this code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?voniospasaulis\.lt$ [NC]
RewriteRule ^.+$ http://www.visaslabas.lt/lt/vonios-iranga/%{REQUEST_URI} [L,R=301]
But I get a result as follow:
www.newdomain.com/en/categoryB/en/categoryA/product1
I need to get rid of /en/before/categoryA to get url like this:
www.newdomain.com/en/categoryB/categoryA/product1
Your rule pattern has to start with en/ and should capture value after en/ in $1 that you can use in target:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?oldodmain\.com$ [NC]
RewriteRule ^en/(.+)$ http://www.newdomain.com/en/categoryB/$1 [L,NE,NC,R=301]
Also note that I have answered using dummy domain names instead of your actual domain names shown in question.
Make sure to clear your browser cache or use a new browser for testing.

htaccess rewrite rule for subdomain results to redirect loop

Below is my htaccess config
RewriteEngine On
# if the domain starts with "apply."
# i.e. apply.domain.com
# redirect to application URI
RewriteCond %{HTTP_HOST} ^apply\. [NC]
RewriteRule !^formwizard /formwizard/apply [R=301,L,NC]
I have tried it, and when I go to http://apply.domain.com/ it successfully redirects to http://apply.domain.com/formwizard/apply.
My problem is, once it redirects to that URI, it goes to a redirect loop.
Can anyone please help hit me what is wrong with my config?
Background:
I have 3 subdomains: www.domain.com, apply.domain.com, and admin.domain.com. It all references to the same source codes and just managed by my htaccess.
Once the person goes to apply.domain.com, it should be redirected to the application page which is /formwizard/apply.
PS. I don't want to depend too much on the backend codes because I believe this is a server config problem.
Thanks in advance!
If there are more rules then this rule might be getting impacted by them. Modify this rule to this:
RewriteCond %{HTTP_HOST} ^apply\. [NC]
RewriteCond %{THE_REQUEST} !/formwizard/ [NC]
RewriteRule ^ /formwizard/apply [R=301,L,NC]
Also make sure this is very first rule below RewriteEngine On line.

Virtual subdomain with wildcard mapping .htaccess

I am building a download site, An users will be able to register and a folder with their username will be created on my server, something like: home/users/username
What I want to accomplish is, if anyone types: username.domain.com in their browser, it will route them to: home/users/username/, and if they type: username.domain.com/file.mp3, it will route them to: home/users/username/file.mp3
If its possible to accomplish sub folders routing, that would be great full aswell, example; home/users/username/sub/file.mp3
Thanks guys
Try adding the following to the .htaccess file in the root directory of your site.
This will rewrite any request to username.domain.com to the correct folder/subfolder and file.
I am assuming that home is a directory in the root folder of your site.
RewriteEngine on
RewriteBase /
#if request for usename.domain.com/anything
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
#send any request that is not already for home/users to home/users/username/anything
RewriteRule ^(?!home/users/) home/users/%1%{REQUEST_URI} [L,NC]
I tried what Ulrich Palha recommended but no luck. I hope this will help guys like me where Ulrich's answer doesn't work.
# Rewrite username.domain.com/<path> to domain.com/home/username/<path>
#
# Rewrite only if not already rewritten to /home/
RewriteCond $1 !home/
# Skip rewrite if subdomain is www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain to %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$
# Rewrite to /home/username/URL-path
RewriteRule (.*) /home/%1/$1 [L]

.htaccess sub domain as file parameter with all the variables

I want to make this url redirect using .htaccess (linux/apache) ..
I want to keep "as is" all calls for www sub domain like this
www.example.com/index.php
www.example.com/test.php?id=1&name=a
www.example.com/whatever.php?whateverurl=xxxx
but I want to redirect all wild card urls like this with the sub domain as extra variable to current unlimited-whatever url variables
for example
xxx.example.com -> example.com/?subdomain=xxx
yyy.example.com/index.php -> example.com/index.php?subdomain=yyy
whatever.example.com/test.php?var=1 -> example.com/test.php?subdomain=whatever&var=1
whatever.example.com/whatever.php?var1=1&var2=2 -> example.com/whatever.php?subdomain=whatever&var1=1&var2=2
is this possible?
Thank you
Once you set up your Wildcard DNS Record you should make your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.com$
RewriteRule ^(.*)\??(.*)?$ $1?host=%{HTTP_HOST}&%{QUERY_STRING} [L] [L]
This rule works, although it will provide you the whole host, rather than just the subdomain, which you'll have to parse in your PHP.
Thank you for your answer
this rule seems to works
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^/?(.*)$ $1?subdomain=%1 [QSA,L,NE]
for the time without problems ...
thanks again for your answer
Best regards
Vangelis

.htaccess Subdomain Rewrite (not like other questions!)

I wish to rewrite admin.example.com/test/ to admin.example.com/index.php?site=test.
I have done ample research and have yet to be able to do so. Regex is not my thing, anyone have suggestions?
(in this particular example, only 'test' is a variable (i.e. 'admin' is constant'))
Thanks!
You could use mod_rewrite to do so:
RewriteEngine on
RewriteRule ^([^/]+)/$ index.php?site=$1
And if you want to restrict that rule to admin.example.com, add this condition:
RewriteEngine on
RewriteCond %{HTTP_HOST} =admin.example.com
RewriteRule ^([^/]+)/$ index.php?site=$1

Resources