Regexp in .htaccess - regex

I've got a simple .htaccess requirement that I'm failing on and would appreciate a pointer.
Content from an old CMS is being migrated to WordPress. The old CMS posts are formatted in the following way:
{postid}-example-post-title
eg
101-this-is-an-old-post
I have a mapping table that links the old post ids to the new WordPress post ids, so in theory \{oldid-*} should map nicely to the WordPress standard \?p={newid} format. I expected the following to work to redirect old url 101-this-is-the-post to the WordPress post 1660:
Redirect 301 /101-.*$ /?p=1660
The regexp /101-.*$ matches correctly, but for some reason my .htaccess file doesn't seem to recognise the regular expression. Here's my full htaccess file:
RewriteEngine Off
SetEnv DEFAULT_PHP_VERSION 56
DirectoryIndex index.cgi index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Redirect 301 /101-(.*) /?p=1660
I've tried moving the Redirect to the top of the file, the bottom of the file and between the IfModule block to no avail. I'm clearly missing something - any thoughts appreciated

As per julp's comment above, the solution to this problem was to use RewriteRule. The following worked correctly, placed withing the IfModule block :
RewriteRule 301 /101-(.*) /?p=166

Related

What is the code Wordpress uses to make SEO friendly URLs?

Can anyone point me to the standard code Wordpress uses to turn a blog post url from this:
http://myblog.com/?p=123
into this:
http://myblog.com/this-is-my-articles-title-i-think
If the post title was: This Is My Article's "Title" (I Think)
WordPress doesn't redirect or rewrite requests. index.php serves the content based on the original request URI. It catches requests that don't exist and figures them out from the plug. This code is in the WordPress document root in .htaccess.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
If you want to know how WP generates the permalink slugs, look in wp-includes/formatting.php for this function:
function sanitize_title_with_dashes($title) { ... }
In your wordpress admin go to settings > permalinks and select 'post name' then save changes and you're all set.

Wordpress: htaccess redirect (301) doesnt work

I have a task which drives me nuts: I have an old blog and want to create a 301 redirect to my new blog for several posts because I moved some of the content to the new domain.
Let's say one of my old posts is to be find at example.com/this-is-my-post and visitors should be redirected to example.org/my-old-post-2817.
What I tried is this:
# BEGIN Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^this-is-my-post$ http://example.org/my-old-post-2817 [R=301]
</IfModule>
# END Redirect
The problem is that there is no redirect in FF (desktop), Chrome (Android) and standard internet browser (Android). In IE (desktop) I get an error stating that the page can't be displayed. No reason is given.
I made sure not to have any unneccessary blank lines in the .htaccess and, to be on the safe side, deleted my browser's histories, caches and so on.
A CCA of my provider dropped me a message that he re-saved the mod rewrite module and that I have to try it 15 minutes later again. With no success.
So, what did I wrong?
Regards,
Crunchy
Change order of rules to keep R=301 rules before internal WP rules:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^this-is-my-post/?$ http://example.org/my-old-post-2817 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

htaccess redirect /foldername/page_name to /page-name

I have an issue:
Recently I replaced my old website (no CMS) with a new website (and WordPress as CMS). But a lot of people still try to access the website by old URL's.
Obviously I want to redirect them to the right page.
For example: the previous URL was: http://domain.com/foldername/page_name
On the new website that same page is located at http://domain.com/page-name
Note #1: The underscore has been replaced with a dash
Note #2: Some pages may include more underscores. For example: http://domain.com/foldername/an_other_page_name
Note #3: Since I use WordPress as CMS, there is already some code in my .htaccess file.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Since I'm not as good with htaccess as some of you I hope someone has the solution, since I'm losing visitors because they can't find the right page.
Just below RewriteBase line you can use this recursive rule:
RewriteRule "^(foldername)/([^_]*)_+([^_]*_.*)$" /$1/$2-$3 [N]
RewriteRule "^(foldername)/([^_]*)_([^_]*)$" /$2-$3 [L,R=301]

.htaccess redirect old html page with space in the name

I've changed an html site to a WordPress installation.
The old .html links have been redirected to the right wordpress page. The old site, which was obviously build by a monkey, had a page /over ons.html. That's right, it had a space in the filename. So now, i'am getting external requests on /over%20ons.html. I can't figure out how i can solve this redirection problem in .htaccess. The page it has to refer to is /over-ons/.
This is my .htaccess so far:
RedirectMatch 301 ^/([^/.]+)\.html$ /$1/
Redirect 301 /over%20ons.html http://www.sterkermerk.nl/over-ons/
Redirect 301 /index.html http://www.sterkermerk.nl/
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The 2nd line contains the rule. I've tried to escape the %20, tried using regex in RedirectMatch, but I can't get it to work.
Anyone got an idea?
Use mod_rewrite instead of mod_alias (Redirect statements) because you're already using mod_rewrite and they'll conflict with each other:
RewriteRule ^over\sons\.html$ http://www.sterkermerk.nl/over-ons/ [L,R=301]
RewriteRule ^index\.html$ http://www.sterkermerk.nl/ [L,R=301]
RewriteRule ^([^/.]+)\.html$ /$1/ [L,R=301]
(replacing the Redirect* statements)
The URI is decoded before being sent through rewrite rules so you don't need to use the %20, just \s or \ will match the space.

301 redirection help on wordpress

I'm searching for a 301 redirect rules in .htaccess for the last one week. I have visited many similar pages here too. But all are not working for me. So I'm asking help from masters like you.
I'm using wordpress blogging platform. Before I was using Joomla and that time so many 404 errors was there on my website. There are more than 10,000s of 404 errors. Mainly two category of 404 errors. Examples are example.com//component/option,com_xmap/Itemid,44/component/xxx/yyy/menu1/menu2/something/ and example.com/index.php/component/option,com_xmap/Itemid,44/component/xxx/yyy/directory/menu1/menu2/something.html. Like these too many urls. I think this happened due to some cache problem in Joomla and these urls were created one by one on the homepage of a module. After I disabled cache for that module the problem was solved, but google indexed all these bad urls already.
I think it's better to do a 301 redirect for those two categories of 404 error urls to the homepage or to www.freezonal.com/sitemap.xml since these urls are only indexed by google and are not seen in search results.
Currently I'm using this on my .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{QUERY_STRING} .*
RewriteRule ^index.php/(.*)$ /? [R=301,L]
Is there any 301 redirection code for redirecting those two categories of 404 urls to the main website and is there any other suggestion for better SEO. I'll be very thankful to you, if you solve this.
First, WordPress' rewrite rules must be on the bottom, because they slurp up any 404s. Anything after them isn't going to work correctly. So add new rules above WordPress' rules.
RewriteEngine On
RewriteBase /
RewriteRule (index.php/|/)?component/option http://example.com/ [L,R=301]
Something like that should work. I don't know if you want to keep any of your /component/ URLs working, so you may need to remove the question mark from the RewriteRule.
In order for the rewrite to work, make sure that you wrap your rewrite conditions in the mod_rewrite.c tags. Then turn turn on RewriteEngine and set your RewriteBase. I don't think the redirects work outside of the mod_rewrite tags.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} .*
RewriteRule ^index.php/(.*)$ /? [R=301,L]
</IfModule>
If you only want to only redirect these two paths to the homepage, try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^component* / [R=301,L]
RewriteRule ^index.php/component* / [R=301,L]
</IfModule>
Try that out and see if it works.
Also, be sure to check out the documentation on apache.org here http://httpd.apache.org/docs/current/mod/mod_rewrite.html