Chad C. Davis
Chad C. Davis

Reputation: 112

Joomla URL rewrite for component without a redirect

I have a Joomla component “com_foo” which works fine when called via...

http://my.joomla.site/index.php?option=com_foo&id=1234

... but I need the URL be in the following format:

http://my.joomla.site/getfoo/1234

The closest I can get is with the following .htaccess rewrite which causes a redirect:

RewriteEngine On

# My Rule
RewriteRule ^getfoo/(.*)$ /index.php?option=com_foo&id=$1 [R=301,L]

#  Joomla rules
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

I can’t seem to figure out how to make the same thing work “internally” w/o the redirect. So in my example above what happens is:

[1] GET http://my.joomla.site/getfoo/1234 REDIRECTS TO
[2] GET http://my.joomla.site/index.php?option=com_foo&id=1234 RENDERS PAGE

What I need is

[1] GET http://my.joomla.site/getfoo/1234 RENDERS PAGE

Almost everything I try results in a 500 error. I feel like it's got to be something simple but it's got me beat at the moment.

Upvotes: 0

Views: 1644

Answers (2)

Brian Bolli
Brian Bolli

Reputation: 1873

Although you could probably get this working using the .htaccess file, by seperating the SEF logic from the component code you open yourself up to a management nightmare down the road. Joomla does however, provide a means to manage SEF url parsing and generation with your custom components. By placing the file router.php in the root of your component's folder, Joomla will automatically use this file to both generate and parse SEF urls. Although the link below does provide detailed information on how to implement, I've found reverse engineering core components router.php file helped me tremendously.

http://docs.joomla.org/Supporting_SEF_URLs_in_your_component

Upvotes: 1

Howli
Howli

Reputation: 12469

Try:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?option=com_foo&id=(.*)\ HTTP
RewriteRule ^ /getfoo/%2\? [R=302,L]

RewriteRule ^getfoo/(.*)$ /index.php?option=com_foo&id=$1 [L]

The above will:

  • Redirect http://my.joomla.site/index.php?option=com_foo&id=1234 to http://my.joomla.site/getfoo/1234 and show the information from the http://my.joomla.site/index.php?option=com_foo&id=1234 page

When viewing

  • http://my.joomla.site/getfoo/1234 will display the information from the http://my.joomla.site/index.php?option=com_foo&id=1234 page

Change the R=302 to R=301 when you are sure the redirect works.

Upvotes: 1

Related Questions