Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

.htaccess rule to do 301 redirects

Discussion in 'Workarounds and Optimization' started by xpresservers, Sep 16, 2012.

  1. xpresservers

    xpresservers Registered

    Sep 14, 2012
    Likes Received:
    Trophy Points:
    cPanel Access Level:
    Root Administrator
    Hello CP Fellows,

    One of my client have a website with thousands of pages. It's wordpress real estate website. Now my client want to change permalink without hurting search engine reputation. The idea became in my mind is to use a wordpress plugin called Redirection. But this plugin creates a lot of log entries (for each hit) in database and make database huge within days. now idea went to .htaccess file. Simple redirection from one URL to another URL is not an issue but issue is having 1000s of webpages which require 301 redirects to new permalinks.

    Old permalink structure: http://domain.tld/kw1/web_page_here/

    Now my client wants: http://domain.tld/keyword2/web-page-here/

    The changes are kw1 to keyword2 and underscores to dashes. Someone made this working in niginx but we do not want to use nginx. We want to use apache as we are using cPanel to manage our servers.

    Following is the code which someone used in nginx to gain desired results and according to him this code works flawlessly.

    Here's what I used:
    fragment in nginx.conf (this will call the kw1-fixup.php file whenever nginx sees kw1 in the URL.)
    location /kw1/ {
                            fastcgi_pass localhost:9011;
                            fastcgi_param REQUEST_URI $request_uri;
                            fastcgi_param SCRIPT_FILENAME /home/user/domains/test.domain.tld/public_html/kw1-fixup.php;
    file kw1-fixup.php (this will rewrite the URI and set a 301 status on the page.)
    $old = $_SERVER[REQUEST_URI];
    $search = array('/kw1/m', '_');
    $replace = array('/keyword2/m', '-');
    $new = str_replace($search, $replace, $old);
    header("Status: 301 Moved Permanently");
    header("Location: http://test.domain.tld$new");

    Any suggestion to achieve this on apache with .htaccess.


Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice