htaccess redirect directories with subdirectories - .htaccess

Have ran into an issue when adding WordPress multisite and then subsequently moving some old posts from the main site to a new subdomain. Those moved posts (their text) carry over fine (using one of several methods available) the problem comes when the front page of the new subdomain looks for featured images it is trying to load
subdomain.example.com/wp-content/uploads/sites/{blog-id}/{dated_subdirectories}
Is there a way to - perhaps using htaccess - forward/redirect those requests to:
example.com/wp-content/uploads/{dated_subdirectories}
I have tried various ways including .htaccess redirect for images from old folder to new folder and I either get a 500 error or the WordPress nothing found page.

Try this rule in wp-content/uploads/.htaccess file:
RewriteEngine On
RewriteBase /wp-content/uploads/
RewriteRule ^sites/[^/]+/(.+)$ $1 [L,R=302,NC]

Related

Redirect existing second page to non-existing url

I have a site https://sitename.com/web (example). So, full path to page is sitename.com/web/index.php. On ftp server there is folder /web which also contain second page file - index-2.php. How to avoid creating /web-2/ folder and redirect index-2.php to sitename.com/web-2 ?
Easy, create an .htaccess file inside the root folder of your site, then add the following lines to it:
RewriteEngine On
RewriteRule ^web-2$ index-2.php [QSA,L,NC]
This site might help you with htaccess:
https://www.htaccessredirect.net/
A nice tutorial on .htaccess files also below:
https://www.digitalocean.com/community/tutorials/how-to-use-the-htaccess-file

htaccess redirect subfolder to another without using root htaccess

I've been searching all day for a solution.
So a friend of my used the horrible web builder to build a basic website in the root directory. For whatever reason, web builder sticks its files in a /site subfolder and it holds the root htaccess hostage. Any changes to web builder overwrite the htaccess.
Now, she has two subfolders. sub1 and sub2. We need to get any attempt to access sub1 to redirect to sub2. I am used to doing this by putting the following code in the root htaccess:
# Enable rewriting.
RewriteEngine on
RewriteRule ^/?sub1/(.*)$ /sub2/$1 [R,L]
But as said, the web builder will erase all of that any time she publishes any changes. I put a new htaccess file in sub1 with the following text:
Redirect 302 / http://example.com/sub2
That redirected the subfolder, but not correctly. It actually redirected to http//example.com/sub2sub1 for some reason that I can't figure out.
I feel like the solution is right in my face but I can't figure it out.
You can use RedirectMatch Directive.
RedirectMatch 302 /sub1/(.*) /sub2/$1

Implementing 301 redirects in sub-folder of Silverstripe

I am trying to create 301 redirects from a silverstripe subdirectory to a new site/subdomain on different server, can't seem to make it work!
So need www.researchnutrition.com.au/practitioner redirecting to www.practitioner.researchnutrition.com.au
along with a number of other urls within the practitioner subfolder specified to new pages on new domain.
Have tried several combinations or rewrite/redirect rules but nothing seems to be taking effect.
Also should the .htaccess file where I make the changes need to sit within the root folder or within the /practitioner folder.
Thanks so much in advance.
You can use .htaccess file in the practitioner folder:
RewriteEngine on
RewriteRule (.*) http://www.practitioner.researchnutrition.com.au/$1 [R=301,L]
Another option is just creating a redirection page in the cms and redirecting that to an external url (which is, despite the option saying "redirect to internal page", possible).

ideas for a 301 redirect wildcards?

I'm having some trouble in finding an answer for a 301 redirect problem. The story goes like this:
I used to have a German language news site at www.punkto.ro. Every new article created with the site's CMS had the form: punkto.ro/articles/title-1234.html, where the 1234 is replaced by the article's number in the database.
For several reasons I had to redesign and opted for wordpress, which I placed in the root. I created a subdomain archive.punkto.ro and replaced the url variable "punkto.ro" in a config.php file with "archive.punkto.ro". Then I moved the site files to the subdirectory "archive". It works fine.
Of course, old article links in google now lead to a 404 response. To be sure, I also indicated the location of the archives on the 404, so people can go look there.
Now to the redirect: what I want to achieve is that when a user clicks a link in google punkto.ro/articles/title-1234.html, he/she should be redirected to archive.punkto.ro/articles/title-1234.html.
The strange stuff is that I can't find the folder "articles" in my file manager (cpanel OR ftp)... Does anybbody have any idea as to how the .htacces should look like?
Examples:
Old link: http://www.punkto.ro/articles/Staatschef_Basescu:_Rumaenien_einschliesslich_gesetzmaessig_fuer_Grexit_gewappnet-4169.html
New link: http://www.archive.punkto.ro/articles/Staatschef_Basescu:_Rumaenien_einschliesslich_gesetzmaessig_fuer_Grexit_gewappnet-4169.html
I don't think you have to create the .htacccess-file in the "articles"-folder. Just create it in the main folder.
I hope this will work for you:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?punkto.ro$
RewriteRule ^articles/(.*)$ http://www.archive.punkto.ro/articles/$1 [L,NC,R=301]
The RewriteCond makes sure that www.archive.punkto.ro will not be redirected again, the RewriteCond redirects every URL containing "articles/" to the archiv.

.htaccess not working in subfolder

I have a couple of sites that are currently under development and I've put them under a specific subfolder for clients/co-workers to view - "http://dev.staffanestberg.com/site-name/". I've run into some problems getting the sites working with .htaccess. I can reach the index page by typing in the url for a site folder but neither img/style/js/etc linking or page rewriting works. Looking for a solution to this, Apache's "RewriteBase" seems to be the most likely one, however I can't get it to work.
Tried
RewriteBase http://dev.staffanestberg.com/site-name/
RewriteBase /site-name
What would be the correct method?
Thanks in advance,
-Staffan
mod_rewrite’s RewriteBase directive does only apply to rewrites using RewriteRule. And that does only apply for requests that are sent to the server.
But if the requested URIs are already wrong, RewriteBase doesn’t do any help. What you could try instead is the HTML element BASE that set’s the base URI for the current document.

Resources