301 Redirect to change structure - .htaccess

I have been researching redirects for a few days now and am still struggling, so I decided to post my first question here. For some reason, it is just not clicking for me.
I have redesigned and developed a client's WordPress site and need to update it's structure.
The site's current structure is:
www.domain.com/blog/postname/2011/12/26/
The new structure should be:
www.domain.com/blog/postname
I really thought this was going to be easy since all I am looking to do is drop the date, but have not been able to grasp the whole wildcard aspect and how to end what I am trying to match. Any help would be greatly appreciated. A simple answer is great, but an explanation would be even better.

I am assuming you already know how to change your WordPress permalink structure to drop the date.
To 301 redirect all of the old URLs to the new ones, add the following rules to your .htaccess file in the root of your websites domain, ahead of any existing rules that are there.
#if these 2 lines already exist, skip them and add the rest
RewriteEngine on
RewriteBase /
# if there is a request of the form /blog/post-name/yyyy/mm/dd/
RewriteCond %{REQUEST_URI} ^(/blog/[^/]+/)[0-9]{4}/[0-9]{2}/[0-9]{2}/$ [NC]
#redirect the request to the URL without the date
RewriteRule . %1 [L,R=301]
If you want to learn more about .htaccess/rewriting you can take a look at the following urls: Indepth htaccess, Brief Introduction to Rewriting, Apache Mod_rewrite.
Let me know if this works for you and/or you have any issues.

Related

htaccess redirect if the url does not contain a specific character

I'm moving the site to a subdomain and need certain tag strings to go to the subdomain and some to remain on the main site. Problem is both have a similar tag system.
I need this type of request
https://www.site.co.uk/tags/example-tag
to go here:
https://sub.site.co.uk/tags/example-tag
but this type of request
https://www.site.co.uk/tags/view?tags=14-some-varriable
to remain unchanged and parsed to content without redirecting.
What would be the most recommended and best solution?
I have written some code to work around other redirects but this one is causing me a headache.
Cheers
For your this mentioned example, below should work.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+)/(.+)
RewriteRule ^ https://sub.site.co.uk/%1/%2 [R]

301 redirect with query strings

I'm new to programming and brand new to this site (although it has helped out many times as I'm trying to learn this stuff...so THANKS for all the help so far!).
My question relates to 301 redirects. I've been searching this site as well as many other pages through google and can't seem to find a solution that works for me (I'm guessing that the solution is probably already out there since it seems like a common problem...but I haven't yet been able to find it).
So here it is:
I have a site where: http://homework-heroes.com/php/views/newAssignment.php?[then any query string] always goes to the same page.
For the sake of eliminating duplicate content as seen by google, I want these to always redirect to: http://homework-heroes.com/php/views/newAssignment.php?final
What is the htaccess code I should insert to accomplish this?
Thanks in advance!
Place the following in your /.htaccess file:
RewriteRngine On
RewriteRule %{QUERY_STRING} !^$
RewriteRule %{QUERY_STRING} !^final$
RewriteRule ^(php/views/newAssignment.php)$ /$1?final [R=302,NC,L]
The above basically says that if the query string is not blank and a request is being made to /php/views/newAssignment.php, then redirect to the same page with ?final as the new query string.
Alternatively, if you would like to remove the query string altogether, just remove final,leave the question mark in the rule, and remove the second condition.
If you're happy and want to make the redirect permanent, change 302 to 301.

Effective Mass URL Forwarding

I'm doing the migration of a site, from MovableType to WordPress. The website contain around 3500 posts and when I uploaded the information to the new version of the site, the links have been altered.
Old site link (note that the category-name and the nodenumber are not fixed values. The nodenumber is a number that change in each post):
http://websitename.com/category-name/nodenumber/title-of-the-post
New site link: http://websitename.com/category-name/title-of-the-post
My question is the following:
What would be the easiest and effective way to configure my Htaccess file to do a redirection from the old links to the new ones? There might be a simple way than to write them all by hand.
If the structure of the URLs is always like this, and the second part is always a number, the following should work in your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.+)/[0-9]+/(.+) /$1/$2 [L]
</IfModule>

htaccess 301 mod rewrite matching url pattern

This one's beyond my ability I'm afraid and after much research and breaking things I've come for help. I have a site with a URL in the format of:
example.com/7865/travel-photo/my-amazing-photo
My client has now decided he'd like to add an 's' onto photos so the URL has become:
example.com/7865/travel-photos/my-amazing-photo
All great and that alone wouldn't have been a problem only for the fact that the number '7865' changes for each URL just like the 'my-amazing-photo' does. The basic structure of the new URL needs to be:
example.com/'number variable'/travel-photos/'article name variable'
So my question how does write a beautiful htaccess mod rewrite rule for that?
RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]+)/travel-photos/(.*) $1/travel-photo/$2

using mod_rewrite to create SEO friendly URLS

I've been searching google for this but can't find the solution to my exact needs. Basically I've already got my URL's named how I like them i.e. "http://mysite.com/blog/page1.php"
What I'm trying to achieve (if it's possible!) is to use rewrite to alter the existing URLS to: "http://mysite.com/blog/page1"
The problem I've come across is I've found examples that will do this if the user enters "http://mysite.com/blog/page1" into the broweser which is great, however I need it to work for the existing links in google as not to loose traffic, so incoming URLS "http://mysite.com/blog/page1.php" are directed to "http://mysite.com/blog/page1".
The 1st example (Canonical URLs) at the following is pretty much what you want:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html#url
This should do the trick, rewriting requests without .php to have it, invisible to the user.
RewriteEngine On
RewriteRule ^/blog/([^.]+)$ /blog/$1.php
You will need to write a rewrite rule for mapping your old url's to your new url as a permanent redirect. This will let the search engine know that the new, seo friendly url's are the ones to be used.
RewriteRule blog/page1.php blog/page1 [R=301,L]

Resources