.htaccess URL rewrite after migrating from Wordpress to Expression Engine - regex

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]

Related

.htaccess adding trailing slashes

I am a beginner web dev and I am attempting to convert one of my old wordpress sites into a vanilla JS/HTML and CSS website.
The URLs on my wordpress site began with www and had trailing slashes at the end of every url (other than the home page). I have been attempting different commands via the .htaccess file I created and I managed to successfully remove the .html extensions from my pages and add the www prefix. However, despite numerous tries (I have tried around twelve different snippets that I found on this website and others) I have not been able to add trailing slashes to the URLs.
I would appreciate any help. Thanks in advance.
The following is my current .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,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").

Rewrite weird difference between servers

Today I wrote the following rewrite rules:
RewriteCond %{HTTP_HOST} ^visionale\.book [NC]
RewriteRule ^\/([^/]+)\/$ $1.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?visionale\.se [NC]
RewriteRule ^(.*)/$ $1.php
The firs is for my laptop where I run Apache, the second rule for the web hotel.
Applying the first rewrite on the web hotel results in a 404 Not Found
Applying the second rule on my laptop mysteriously results in "pagename.php.php.php" not found.
Now the solution I provided works at both locations but I am baffled about what really is happening. My local machine runs Apache 2.4.6. The web hotel runs Litespeed. So this is an incompatibility of course, but exactly how.
My guess is that Litespeed misses the first rule because of missing functionality, but why does Apache not get the easier one, that I've used many times in the past?
Edit: Clarifying the questions.
This rule:
RewriteRule ^\/([^/]+)\/$ $1.php [L]
Is not picked on my web hotel running Litespeed. My guess is that this is because Litespeed has a flawed rewrite implementation. I'd like to get that hunch confirmed or another explanation privided.
This rule:
RewriteRule ^(.*)/$ $1.php
Does not work on my dev-machine any more but it has worked in the past. It seems simple enough. Anything ending with a trailing slash should instead get a ".php" extension. However, on my dev machine it adds three ".php" instead of one. The rule works as intended in the web hotel and it has worked for me locally in the past. This is puzzling and I would like an explanation.
Both rules are incorrect. Let me provide you correct code first:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^visionale\.book$ [NC]
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ $1.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?visionale\.se$ [NC]
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ $1.php [L]
Now the problems in your code:
RewriteRule in .htaccess doesn't start matching with leading slash which is striped by mod_rewrite
You need to avoid rematching these rules using RewriteCond %{REQUEST_FILENAME} !-f otherwise mod_rewrite will keep adding .php in the URI.

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.

RewriteRule not working when file extensions included

Hey, I am trying to use mod_rewrite to make http://example.com/style/universal.css show the page http://example.com/style.php?n=universal
I am currently using the following RewriteRule:
RewriteRule ^style/([^/\.]+)\.css$ style.php?n=$1
But it doesn't seem to work, for some reason it instead shows a 404 Not Found which is not even the correct 404 page.
I have tried doing it without the extension .css (i.e. http://example.com/style/universal) with the RewriteRule
RewriteRule ^style/([^/\.]+)$ style.php?n=$1
Which works, but I would much prefer it if I could get it working with the .css extension.
It seems to be something with the server ignoring the RewriteRule if a file extension is used. Is there a RewriteCond or something obvious that I am missing?
I should also mention that there is a .htaccess in the parent directory which is set by WordPress, the contents of this are:
RewriteEngine on
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Any help would really be appreciated.
Thanks in advance, Brad.
Set up your mod_rewrite to log all the steps it does processing your url
RewriteLog "/tmp/mysite_rewrite.log"
RewriteLogLevel 6
reading the logs you should be able to understand how your url is rewritten and what is the problem.