Blog Page 404 - regular expression for rewrite needs fix - regex

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").

Related

Fixing image urls after nightmare WP migration

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

URL Rewrite regex, can't seem to get it to not force the include of www.'s

I've got the Hellicon Tech URL rewrite ISAPI filter on my server. I've got a global file that dictates the settings across the server with the following.
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]
Basically it forces any request coming to the site to apply a "www." to prefix the URL that's being requested.
I've created a specific set of rules for a site on my server, but each time I attempt to modify the pattern to not force the www.'s it either breaks simply doesn't work.
I've narrowed this down to being specifically around the regular expressions, however I cant' seem to get it right.
Any suggestions on making this work would be greatly appreciated, still wanting to keep the https if the domain is requiring it, but trying to eliminate the www. prefix wont seem to work.
I can't test this with Hellicon's ISAPI, but have you tried something simpler?
RewriteCond %{HTTP:Host} !^www\. [NC]
RewriteCond %{HTTPS}:s (on:(s)|off:s)
RewriteRule ^(.*)$ http%2://www.%{HTTP:Host}/$1 [R=301,L]
remember to clear your browser's cache before testing, the 301 redirect is permanent and your browser will cache the redirected target.

Redirect all requests to index.php except subdomains

I'm using AltoRouter: (http://altorouter.com/) and I modified .htaccess as suggested in the instalation to:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
This is so that index.php can handle all the requests. My problem is that I am using addon domains in cpanel and I am having internal server errors when I try to access one of the domains associated with the addon domain.
Example:
My main domain is mainsite.com.
Then I have several sites:
site1.com that cpanel automatically associates with site1.mainsite.com and creates a folder mainsite.com/site1.com. So if I access site1.com I would see in browser site1.com but the content delivered would be the one inside the mainsite.com/site.com folder.
This works if I don't use the .htaccess rule but I need it for routing. If I have that rule I get internal server errors everytime I access site1.com (I assume that it's a conflict between cpanel rules and .htaccess).
How can I modify the rule so that it only affects maindomain and not subdomains? I am assuming that by doing this there would be no conflict and my problem would be solved.
I am really bad at .htaccess and regex but I am willing to learn if needed. I would still appreciate if you could point me to the right direction. (both in the idea and in good websites that can help me understanding this)
How can I modify the rule so that it only affects maindomain and not subdomains?
You can add a new condition based on host name:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?mainsite\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

.htaccess URL rewrite after migrating from Wordpress to Expression Engine

I'm helping a client move their blog from a separate Wordpress installation to part of the overall Expression Engine installation for their new (and very large!) website.
The old url structure for the blog was www.site.com/blog/yyyy/mm/foo-bar-title
The new URL structure will be www.site.com/blog/article/foo-bar-title
The .htaccess file isn't that complex so far, essentially it's this:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png|xml)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
And try as I might I can't seem to find a rewrite method that gets the URL rewritten with a 301 redirect without causing an infinite loop.
So far I have RewriteRule ^(.*)\[0-9]+/[0-9]+/?$ /blog/article/ [R=301] but that causes the infinite loop. I've looked at the other questions and answers, but they all seem to deal with just Wordpress (when you search for wordpress), although strictly speaking this won't involve wordpress at all and just Expression Engine.
All help very much appreciated!
You may find it helpful to turn on Apache Server's rewrite log while you tinker with this.
I think you are looking for something like this:
RewriteRule ^index.php/blog/[0-9]+/[0-9]+/(.*)$ /blog/article/$1 [R=301]

Redirect traffic that doesn't match a particular path

I have an old site which is no longer used and need to redirect traffic elsewhere. One problem I have though is that one particular area of the site needs to remain available to support previous customers who have purchased items from the site.
I would like to do this with .htaccess but my regex knowledge is quite limited.
Basically I would like to redirect all site requests which do not have the support path (which can have additional segments on the end):
http://www.example.com/support
http://www.example.com/support/contact
http://www.example.com/support/ticket
However the following examples (two of many) should be redirected to the new site:
http://www.example.com
http://www.example.com/projects
So far I have only figure out how to redirect requests with no additional parameters using:
RewriteRule ^$ http://www.newsite.com/ [L,R=301]
I know that what I need is basically something like this:
IF url does not contain www.example.com/support
THEN redirect
I just don't know how to write it with regex / .htaccess
Any help would be appreciated.
It is easier with mod_rewrite directives, like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteCond %{REQUEST_URI} !/support/? [NC]
RewriteRule ^(.*)$ http://www.newsite.com/$1 [L,R=301,QSA]
Apache has great documentation for mod_rewrite. It really is worth a read.
In your special case, to avoid a redirect for all paths starting with support, you can use a RewriteCond directive, i.e.:
RewriteCond %{REQUEST_URI} !^/support
RewriteRule ^(.*) http://www.newsite.com/$1 [L,R=301]
Try this:
RewriteRule ^(support/?.*)$ $1 [L]
RewriteRule ^(.*)$ http://www.newsite.com/$1 [L,R=301]
The first one "forwards" your support/ request to support/... and finishes the processing of the RewriteRules. And the second should forward all your other queries to the new site.