Rewriting old WordPress urls to new site's URL structure via .htaccess - apache

Hey folks, I've migrated a site from WordPress to a new CMS, and I want to preserve the old URLs via a redirect.
The WordPress permalink structure was like so:
/2011/04/01/name-of-post
I've preserved the post slugs, so all I need to do is get rid of the date-based paths and redirect to my new directory structure:
/articles/view/name-of-post
My attempts thus far have looked like this (in my .htaccess file):
RewriteCond %{THE_REQUEST} /[0-9]{4}/[0-9]{2}/[0-9]{2}/(.+) [NC]
RewriteRule ^/[0-9]{4}/[0-9]{2}/[0-9]{2}/(.+)$ /articles/view/$1 [L]
No luck yet. I tried %{PATH_INFO} in there as well, no dice.
Any help from those more versed in Apache rewrite rules than would be much appreciated.

Something like this should do.
RedirectMatch permanent ^/20../../../(.*)$ /articles/view/$1

It can be handled in one simple RewriteRule like this:
RewriteRule ^[0-9]{4}/[0-9]{2}/[0-9]{2}/(.+)$ /articles/view/$1 [L]

Related

Rewrite resource paths to new subdomain

We moved a main site advancedbrain.com to a new site/server. The old site still serves up some older content. It can now be accessed at this subdomain: l.advancedbrain.com.
Many of the old pages on the old server are still looking for resources using paths like:
http://advancedbrain.com/components/com_ixxocart/images/buttons/en/buttonAddToCart.gif
http://advancedbrain.com/templates/abthome/images/abt_logo.png
http://advancedbrain.com/plugins/system/2j_tabs/2j_tabs.css
http://advancedbrain.com/components/com_ixxocart/content/ajax/xajax_js/xajax.js
etc. etc.
I am trying to write a rewrite in .htaccess where it will look for those resources on l.advancedbrain.com instead of advancedbrain.com.
I have tried a few things but they haven't worked.
Finally figured this out. Here it is in hopes it will help others.
RewriteEngine On
RewriteRule "^/components/com_ixxocart/images/(.+)" "http://l.advancedbrain.com/components/com_ixxocart/images/$1" [NC,L,R=301]
RewriteRule "^/templates/abthome/images/(.+)" "http://l.advancedbrain.com/templates/abthome/images/$1" [NC,L,R=301]
RewriteRule "^/plugins/system/2j_tabs/(.+)" "http://l.advancedbrain.com/plugins/system/2j_tabs/$1" [NC,L,R=301]
RewriteRule "^/components/com_ixxocart/content/(.+)" "http://l.advancedbrain.com/components/com_ixxocart/content/$1" [NC,L,R=301]

Apache htaccess rewrite (Pretty URLs)

I have a sneaking suspicion this is not possible, but figured I would ask regardless.
Is it at all possible to take a URL passed to a server in the form of:
http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3
And rewrite it to appear as:
http://domain.com/Controller/Action/1/2/3
I am trying to clean up an borderline ancient project to support "Pretty URLs" and I would really like to make the URLs display a bit nicer. I know I could setup a 301 header redirect to the new URL, but I would prefer to avoid that overhead if at all possible.
Any thoughts?
Thanks!
To get
http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3
To appear as
http://domain.com/Controller/Action/1/2/3
You will need to use %{QUERY_STRING} to capture the query string data. Your .htaccess file will look like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^Action=Controller/Action&one=(\d+)&two=(\d+)&three=(\d+)
RewriteRule ^.+ /Controller/Action/%1/%2/%3 [R=301,L]
This will set up a permanent redirect to the new page. You can play around and test .htaccess rewrite rules here: htaccess.madewithlove.be
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?Action=$1/$2&one=$3&two=$4&three=$5 [L,QSA]

.htaccess mod_rewrite 301 redirect with nested exceptions?

I need a little help with my .htaccess before I deploy it!
I want to 301 redirect almost everything from elementalthreads.com to ethreads.com, excluding blog/wp-content/uploads, and /pommo.
Am I doing this right?:
RewriteEngine on
#exclude old uploads folder and /pommo
RewriteCond %{REQUEST_URI} !^/(blog/wp-content/uploads|pommo) [NC]
RewriteRule (.*) http://ethreads.com/$1 [R=301,L]
Will that transfer canonical pagerank?
Here's where I know I need help:
The old site has a wordpress blog, which I've cloned on the new domain. I'd love to preserve the permalinks, which are almost 1:1, eg:
http://www.elementalthreads.com/blog/ethreads-now-on-amazon-com/ redirects to
http://ethreads.com/ethreads-now-on-amazon-com/ (note /blog/ is missing here)
And the blog index http://www.elementalthreads.com/blog/ should redirect to http://ethreads.com/blog/, which seems like an exception to the above rule, since "/blog/" should only be preserved here?
I'm stumped about how to regEx or otherwise define these last two conditions/rules. Any help would be most appreciated!
That looks correct to me. However, you should not put this live without checking it, there really is nothing preventing you from being able to test it. One thing to bare in mind is that browsers can cache 301 response codes so when testing you should use [R,L] as your flags. Once you are happy add the [R=301,L] back in before deployment.
OK for points (1) & (2)
# only redirect the blog direcotry
RewriteRule ^blog/?$ http://ethreads.com/blog/ [NC,R=301,L]
# redirect all sub folders of blog to the new domain
RewriteRule ^blog/([\w-])/?$ http://ethreads.com/$1/ [NC,R=301,L]

301 redirect query string to SEO friendly URLs through .htaccess

I’ve written some code on my .htaccess file which allows the use of SEO friendly URLs instead of ugly query strings. The following code rewrites the SEO friendly version in the browser to the query string version on the server.
RewriteEngine On
RewriteRule ^seo/([^/]*)/$ /directory/script.php?size=large&colour=green&pattern=$1 [L]
So that the ugly
http://www.mysite.com/directory/script.php?size=large&colour=green&pattern=striped
Is now beautiful
http://www.mysite.com/directory/seo/striped/
Just to explain the code a bit; seo is there to add more keywords to the URL, /directory/ is the directory in which the .htaccess file is located, parameters size=large and colour=green never change, while pattern=$1 can be many different values.
The above code works perfectly. However, the problem is I am now stuck with two URLs that point to exactly the same content. To solve this, I would like to 301 redirect the old, ugly querystrings to the SEO friendly URLs. What I have tried so far does not work - and Google is not being particularly friendly today.
Can anybody offer working code to put in my .htaccess file that redirects ugly to new URL, while retaining the rewrite? Thanks!
This should do the trick:
RewriteEngine On
## Redirect to pretty urls
# The '%1' in the rewrite comes from the group in the previous RewriteCond
RewriteCond %{REQUEST_URI} !seo
RewriteCond %{QUERY_STRING} ^size=large&colour=green&pattern=([a-zA-Z]*)$
RewriteRule (.*) /directory\/seo\/%1\/? [L,R=301]
## Rewrite to long url, additional parameter at the end will cause
## the internal redirect not to match the previous rule (would cause redirect loop)
RewriteRule ^directory\/seo\/([^/]*)/$ /directory/script.php? size=large&colour=green&pattern=$1&rewrite [L]
You can also match the size and colour if needed, by changing those to regex groups as well, and using the corresponding %N
Hope this helps.
Not tested, but this may work...
RewriteRule ^directory/script.php?size=large&colour=green&pattern=(.*)$ /seo/$1/? [R=301,NE,NC,L]

mod_rewrite to change my urls

I've been fighting with mod-rewrite for a while.
Basically, I have a website that I'm moving to a difference namespace/directory.
What I'd like to do is change urls that look like this:
http://mydomain.com/index.php?a=xxxxxxxxxx
These urls will always have "index.php?a=". I have a different/new site that also has an index.php file, so it's important that I do a rewrite only when a= is in the URL.
The new url should be like
http://mydomain.com/ns1/index.php?a=xxxxxxxxxxx
Seems pretty simple, but i can't seem to get mod_rewrite to do it for me, here's what I have:
# rewrite old urls to new namespace
RewriteRule ^/index.php\?a=(.*)$ /gc1/index.php\?x=1&a=$1 [R=301,L]
See anything wrong?
You can use this rule to add a prefix to the path:
RewriteRule !^ns1/ /ns1%{REQUEST_URI} [L]
This rule prefixes the URI path with /ns1 if the path is not already starting with it.
I believe that RewriteRule is only valid for paths, (I.e. it won't take into account any query parameters that you add.)
You are probably better off doing this in the PHP file itself.
// On old site
if($_GET['a']=="xxxxxxx"){
header("Location: /ns1/index.php?a=".$_GET['a']);
die();
}
Thanks to both of you.
I was able to do it:
# rewrite old urls to new ns1 namespace
RewriteCond %{QUERY_STRING} ^a=.*$
RewriteCond %{REQUEST_URI} !^/ns1/.*$
RewriteRule ^(.*)$ ns1$1 [R=301,L]