开发者

CakePHP remove "?url=" from the URL after a redirect

开发者 https://www.devze.com 2023-03-22 02:44 出处:网络
I\'m using CakePHP for my PHP application. By default it puts this into a .htaccess file: RewriteR开发者_如何学编程ule ^(.*)$ index.php?url=$1 [QSA,L]

I'm using CakePHP for my PHP application.

By default it puts this into a .htaccess file:

RewriteR开发者_如何学编程ule ^(.*)$ index.php?url=$1 [QSA,L]

After this rule, i am doing this:

RedirectMatch ^/resources(.*)$ http://domain.com/community$1 [QSA,R=permanent,L]

This makes the redirected URL:

http://domain.com/community/view/171/?url=resources/view/171/

How can i get rid of the appended "?URL=" without breaking cakephp's routing?


If I understand your question correctly, you want to have requests for /community/* routed to ResourcesController. You should be able to acheive this by adding the following to your app/config/routes.php.

/**
 * Rename /resources/* to /community/*
 */
Router::connect('/community', array('controller' => 'resources'));
Router::connect('/community/:action/*', array('controller' => 'resources'));

The second rule does most of the magic, mapping matching requests to the ResourcesController and passing in the action also.

With the above approach you can also take advantage of reverse-routing:

echo $this->Html->link('View community', array(
    'controller' => 'resources',
    'action' => 'view',
    $id
));
// outputs a link to `/community/view/171`

The first rule is simply there to keep the action name out of the root URL (ie. so HtmlHelper links that are reverse-routed become /community instead of /community/index).


Following up on LazyOne's comment, if you are also looking to redirect old /resources*-style links for SEO purposes, the following should do the trick:

# app/webroot/.htaccess
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^/resources(.*)$ /community$1 [R=301,L] # permanent redirect
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
0

精彩评论

暂无评论...
验证码 换一张
取 消