Search code examples
.htaccessmod-rewritejoomlaurl-rewritingjoomla-k2

Redirect url from Joomla articles to K2 articles using htaccess


I've imported my articles from Joomla! to K2 keeping the ids the same. Now I'd like to rewrite the Joomla! urls to point to the K2 items. I'm using Joomla 2.5.9 with SEF and modrewrite enable. Also, I've never worked with modrewrite rules I think it's the best option as I've thousands and thousands or articles.

The url I want to change is as:

http://www.mysite.com/news/component/content/article/9-society/19771-the-alias-of-the-article

And the url generated by the same K2 item is

http://www.mysite.com/news/society/19771-the-alias-of-the-article

I created the menu item society in order to get this url.

So, i edited the .htaccess to create a rule as follow

RewriteRule ^news/component/content/article/9-(.*)$ /news/$1 [R=301,L]

But i get a 404 error page. Note that this example is a try to understand if the modrewrite is working. I've not taken into acount the id category (9 in this example).

The question is: What did I miss with the rewrite rule?

In addition, I've tried other rules which I have seen in SO and also googling trying to understand how they work but also with the 404 error page. The rule provided in this question is for my little knowledge the one that should do the magic.

The htaccess file with the modifications suggested in the answers is as follows (My site is in the news folder. If I use RewriteBase I need to adjust it):

##
# @package    Joomla
# ...
Options +FollowSymlinks -MultiViews

## Mod_rewrite in use.
RewriteEngine On

## Begin - Rewrite rules to block out some common exploits.
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*([^)]*) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
## Begin - Custom redirects
# My site is in the news folder. If I use RewriteBase I need to adjust it
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news/component/content/article/9-([^/]+)/([^/]+)/? /news/$1/$2 [R=301,L]
## End - Custom redirects
##
# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##
# RewriteBase /
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index.php
# and the request is for something within the component folder,
# or for the site root, or for an extensionless URL, or the
# requested URL ends with one of the listed extensions
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|.(php|html?|feed|pdf|vcf|raw))$ [NC]
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.

Solution

  • You may try this in one .htaccess file in root directory:

    Updated for .htaccess file in folder /news:

    Options +FollowSymlinks -MultiViews
    RewriteEngine On
    RewriteBase /news
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^component/content/article/9-([^/]+)/([^/]+)/?  $1/$2 [R=301,L]
    

    Redirects

    http://www.mysite.com/news/component/content/article/9-society/19771-the-alias-of-the-article with or without trailing slash

    To

    http://www.mysite.com/news/society/19771-the-alias-of-the-article

    All strings are assumed to be fixed, except society and 19771-the-alias-of-the-article.

    For silent mapping, remove R=301 from [R=301,L]

    NOTE: Both RewriteCond %{REQUEST_FILENAME}... are there to prevent loops. They check if the file or the directory exists, to skip the rule in case they do.