4

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.
pQB
  • 3,077
  • 3
  • 23
  • 49
  • 1
    I think you need both a `RewriteCond` and a `RewriteRule`...although perhaps you already have a `RewriteCond` earlier in .htaccess file (I think maybe they have to appear on adjacent lines though), in which case it would be helpful to post that here as well. – Matt Browne Feb 25 '13 at 00:22

1 Answers1

2

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.

Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
  • I tried your answer in one .htaccess file in root directory and clean all the browers history but the `404 page not found' persist. I also tried the rule in the `.htaccess` provided in Joomla! 2.5.9 with the same result. Any suggestion ? – pQB Feb 25 '13 at 22:54
  • Yes, please copy-paste the complete set of rules. Maybe you are missing the options line, that I just updated with `MultiViews`. – Felipe Alameda A Feb 26 '13 at 03:02
  • I was missing the MultiViews option. Added but still I get a 404 error. I added the htaccess file to the question. I tried your standalone answer in a file in the root directory. The attached file in the question is in the `news` folder where Joomla! is installed. – pQB Feb 26 '13 at 08:35
  • @pQB I see you added this rule-set to the file in your question and the RewriteBase is `/news`. I updated my answer accordingly. That should do it. If still fails, most probably there is a conflict with the other rules in the file. – Felipe Alameda A Feb 26 '13 at 10:35
  • I see. The problem was basically with the rewritebase. It's working now. You rock. – pQB Feb 26 '13 at 10:43
  • I'd appreciate if you extend the answer explaining the use of RewriteCond – pQB Feb 26 '13 at 10:45
  • @pQB Done. Hope that explains the reason to include them in the rule-set. – Felipe Alameda A Feb 26 '13 at 10:52
  • Sure, prevent `infinity loops` looks a good reason to keep the RewriteCond :). I'll keep reading about rewrites rules. Thanks. – pQB Feb 26 '13 at 10:57
  • Hi.I have the same problem.My url is this now: `mysite.com/sayan/component/virtuemart/t-shirt-s/66-n-t-shirt-49-detail?Itemid=0` Now I want to make it like this: `mysite.com/sayan/66-n-t-shirt-49-detail?Itemid=0` What should I write exactly to achieve this url? – Hamid Reza Nov 20 '13 at 06:53