Fixing image urls after nightmare WP migration - regex

I'm at the tail-end of a nightmare WP migration. Got everything working except a bunch of broken image links, which I've tracked down to a repeating token.
example (broken) URL is:
http://localhost/wordpress/wp-content/uploads/2016/06/reset.gif
and the actual gif is located in:
http://localhost/wordpress/wp-content/uploads/2016/06/06/reset.gif
With hundreds of such instances I thought the easiest solution would be a RewriteRule in the .htaccess file to handle the redirect, but I can't nail down the regex to handle it. Any help appreciated.

You don't actually need mod_rewrite to do this.
Following the regex suggested by Sato Katsura (i.e. I haven't tested the match) you could do this, as suggested by the linked wiki page:
RedirectMatch ^(/wordpress/wp-content/uploads/[0-9]{4}/)([0-9]{2}/)(.*) $1$2$2$3

The example provided needs the month twice, so you could get along with:
\/(\d+)\/.+$
See a demo on regex101.com.
In terms of RewriteRule:
RewriteEngine On
RewriteRule ^(.+)\/(\d+)\/(.+)$ $1/$2/$2/$3 [R=301,L]
See a demo for the replacement on regex101.com as well.

Try Search & Replace plugin ( or interconnectit database search and replace script ) before tampering with .htaccess and mod_rewrite. Tools like that are essential for fixing broken links during transfer or a migration of an WordPress.
The location of the image files uploaded through the WordPress media upload system is in wp_postmeta table. The problem is therefore in the database and there should be resolved.

I faced the same issue while I migrated WP Website to the new server. Following should help you and let me know if you still face any more troubles
just you need to ensure your root folder has Acess permission as 755 Permissions.
You should add the below lines in your wpconfig.php file in your wp root-
define('WP_HOME','https://covid19.pinlocos.com');
define('WP_SITEURL','https://covid19.pinlocos.com');
Add a .htaccess file with the following details:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
ErrorDocument 404 index.php

Related

Redirect with htaccess directory/page to directory under different domain

I need to redirect whole website from one domain to another with the exact extention, BUT only all pages in specific directory should be redirected to another directory on new website.
http://www.website.com/dir/page-123 (all pages starting with "page-" to http://www.newwebsite/dir/.
Tried many options, nothing works.
Please shot the ideas
Try this Rule
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/dir/page-([\d]+)$
RewriteRule ^ http://www.newwebsite/dir [R=301,NC]
if your page have extension please use
RewriteCond %{REQUEST_URI} ^/dir/page-([\d]+).exe$
replace exe with your extension.

How can I redirect all visitors to a subdirectory but still have access to the root directory for development

I'm about to start building a site and I've set up an "Under Construction" site at http://positivechange.cl
I'd like to work directly on the root directory during development so that I don't have to move all files from a development subdirectory to the root directory, so I'd like to redirect users to something like http://positivechange.cl/maintenance
After searching online I know how to redirect to the subdirectory using RedirectMatch ^/$ /subdirectory/ on the .htaccess file, but that doesn't allow me to access the root directory and check how the site is going.
Is there any way to do this?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !(^|&)dev=1(&|$) [NC]
RewriteRule !^maintenance\.html$ maintenance.html [L,NC]
Key is use of query parameter dev=1. Without this query parameter site will show maintenance.html. But if you add ?dev=1 to your URL then you can access your development website.
Because you already using redirect...
try comment that line and tell me how it's goes.
Hope this helpful cheers~

Apache Rewrite for images on subdomain

I have a webserver and a subdomain. All of my images are stored in /public/images within my site directory for www.mysite.com. However I have a separate site directory for testing beta.mysite.com however on this page with a different git branch all of my images are broken because I did not want to copy all of the images. Is it possible to say for all image requests or for all 404 requests try looking at mysite.com?
I have found an example on another questions
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^images/(.*)$ http://example.com/sub_ds/test/images/$1
But since I am rather new to mod_rewrite im not sure what is going on or how to manipulate it to work for me.
You can use this rule in root .htaccess of beta subdomain:
RewriteEngine On
RewriteCond %{HTTP_HOST} =beta.example.com
RewriteRule ^(images/.+)$ http://example.com//public/$1 [L,NC,R=301]

Blog Page 404 - regular expression for rewrite needs fix

I've got a regular expression head-scratcher and could really use some advice from someone that knows regex. My URL rewrite rule below rewrites URL requests for /blog/customurl to /blog/index.php/customurl - this works great for Wordpress permalinks.
Rewrite Engine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/blog/(?!index.php)(?!wp)([^\.]*)$ /blog/index.php/$1 [I]
Now I also need a separate rule that would rewrite /blog or /blog/ (nothing trailing) as /blog/index.php without affecting that existing rule. Requests for /blog or /blog/ 404 right now, but adding index.php manually in the address bar pulls up the blog home page. Ideas?
FYI In this case, I can't set up folder-specific rewrite rules using the .htaccess in /blog/ . I also can't use RewriteBase, so I'm really hoping for a solution that tackles this from the root folder with a regular expression.
You're almost there. Try this:
RewriteRule ^/blog/?$ /blog/index.php$1 [L]
This just says that if the URL is /blog or /blog/, go to /blog/index.php (and the [L] means "don't process any more rules").

Apache htaccess redirect directory and some contents

I'm currently trying to get Apache to redirect any requests for a directory or it's contents to a single file. The directory in question is /press. This happens to contain multiple images that are being used by other pages on the site. I want to redirect /press and /press/* to /press-materials/ but allow any image requests (e.g. /press/logo.png) to be completed. I've tried a few solutions and get one aspect of the redirect working but it doesn't meet the other criteria.
I'm a novice when it comes to Apache and REGEX (Something I knowingly need to work on) so this may be a simple fix.
Here is the htaccess call that redirects /press to /press-materials but /press/filename returns a 404 error.
RewriteRule ^press/$ /press-materials/ [R,L]
After 6 hours that's as far as I've gotten. Any help would be most appreciated.
Assuming that you want to allow all requests to files which actually exist in /press (like /press/logo.png):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^press/.* /press-materials/ [R,L]
This performs the rewrite only if no existing file in /press has been requested.
Another solution to allow only specific file types (e.g. only *.jpg, *.png, *.gif) and only if they are existing in the /press directory but not in a subdirectory:
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} !^/press/[^\/]+\.(png|jpg|gif)$
RewriteRule ^press/.* /press-materials/ [R,L]
More information about rewrite conditions can be found here.