没有重定向的组件的Joomla URL重写

时间:2014-04-01 19:10:10

标签: .htaccess mod-rewrite redirect joomla

我有一个Joomla组件“com_foo”,通过...调用时工作正常。

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

...但我需要网址采用以下格式:

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

我能得到的最接近的是.htaccess重写导致重定向:

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]

我似乎无法弄清楚如何使同样的事情在“内部”工作而没有重定向。所以在上面的例子里发生的事情是:

[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

我需要的是

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

我尝试的几乎所有内容都会导致500错误。我觉得它必须是简单的东西,但它让我在此刻被击败。

2 个答案:

答案 0 :(得分:1)

尝试:

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]

以上将:

  • http://my.joomla.site/index.php?option=com_foo&id=1234重定向至http://my.joomla.site/getfoo/1234并显示http://my.joomla.site/index.php?option=com_foo&id=1234页面中的信息

查看时

  • http://my.joomla.site/getfoo/1234将显示http://my.joomla.site/index.php?option=com_foo&id=1234页面
  • 中的信息

当您确定重定向有效时,将R=302更改为R=301

答案 1 :(得分:1)

虽然你可能会使用.htaccess文件来解决这个问题,但是通过从组件代码中分离出SEF逻辑,你可以自己打开管理噩梦。但是,Joomla提供了一种方法来管理SEF url解析和使用自定义组件生成。通过将文件router.php放在组件文件夹的根目录中,Joomla将自动使用此文件来生成和解析SEF URL。虽然下面的链接提供了有关如何实施的详细信息,但我发现逆向工程核心组件router.php文件对我帮助很大。

http://docs.joomla.org/Supporting_SEF_URLs_in_your_component