1 (edited by zetrader 2019-02-08 18:27)

Topic: Redirect 301 in htaccess with url using schema file based (fancy)

Hello, if i plan to change my blog to a forum, i need to redirect some urls to the topics urls, but i have a problem in htaccess, it seems like it ignores the 301 redirect instruction (instructions that works with the htaccess of my cms blog, to move an url to another place).

Here's the htaccess code of punbb :

# BEGIN PunBB

# ----------------------------------------------------------------------
# Start rewrite engine
# ----------------------------------------------------------------------

<IfModule mod_rewrite.c>
    # MultiViews interfers with proper rewriting
    Options -MultiViews

    RewriteEngine On

    # Uncomment and properly set the RewriteBase if the rewrite rules are not working properly
    RewriteBase /myfolder

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . rewrite.php [L]
</IfModule>

# ----------------------------------------------------------------------
# Better website experience for IE users
# ----------------------------------------------------------------------

# Force the latest IE version, in various cases when it may fall back to IE7 mode
# github.com/rails/rails/commit/123eb25#commitcomment-118920
# Use ChromeFrame if it's installed for a better experience for the poor IE folk

<IfModule mod_setenvif.c>
     <IfModule mod_headers.c>
        BrowserMatch MSIE ie
        Header set X-UA-Compatible "IE=Edge,chrome=1" env=ie
      </IfModule>
</IfModule>

<IfModule mod_headers.c>
    # Because X-UA-Compatible isn't sent to non-IE (to save header bytes),
    # We need to inform proxies that content changes based on UA
      Header append Vary User-Agent
    # Cache control is set only if mod_headers is enabled, so that's unncessary to declare
</IfModule>


# ----------------------------------------------------------------------
# UTF-8 encoding
# ----------------------------------------------------------------------

# Use UTF-8 encoding for anything served text/plain or text/html
AddDefaultCharset utf-8

# Force UTF-8 for a number of file formats
AddCharset utf-8 .html .css .js .xml .json .rss


# ----------------------------------------------------------------------
# A little more security
# ----------------------------------------------------------------------

# Do we want to advertise the exact version number of Apache we're running?
# Probably not.
## This can only be enabled if used in httpd.conf - It will not work in .htaccess
# ServerTokens Prod


# "-Indexes" will have Apache block users from browsing folders without a default document
# Usually you should leave this activated, because you shouldn't allow everybody to surf through
# every folder on your server (which includes rather private places like CMS system folders).
<IfModule mod_autoindex.c>
    Options -Indexes
</IfModule>

# END PunBB

the rewrite.php has this code :

<?php
/**
 * Rewrites SEF URLs to their actual files.
 *
 * @copyright (C) 2008-2012 PunBB, partially based on code (C) 2008-2009 FluxBB.org
 * @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
 * @package PunBB
 */


define('FORUM_ROOT', './');
require FORUM_ROOT.'include/essentials.php';

// Bring in all the rewrite rules
if (file_exists(FORUM_ROOT.'include/url/'.$forum_config['o_sef'].'/rewrite_rules.php'))
    require FORUM_ROOT.'include/url/'.$forum_config['o_sef'].'/rewrite_rules.php';
else
    require FORUM_ROOT.'include/url/Default/rewrite_rules.php';

// Allow extensions to create their own rewrite rules/modify existing rules
($hook = get_hook('re_rewrite_rules')) ? eval($hook) : null;

// If query string is not set properly, create one and set $_GET
// E.g. lighttpd's 404 handler does not pass query string
if ((!isset($_SERVER['QUERY_STRING']) || empty($_SERVER['QUERY_STRING'])) && strpos($_SERVER['REQUEST_URI'], '?') !== false)
{
    $_SERVER['QUERY_STRING'] = parse_url('http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
    $_SERVER['QUERY_STRING'] = isset($_SERVER['QUERY_STRING']['query']) ? $_SERVER['QUERY_STRING']['query'] : '';
    parse_str($_SERVER['QUERY_STRING'], $_GET);
}

// We determine the path to the script, since we need to separate the path from the data to be rewritten
$path_to_script = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));
if (substr($path_to_script, -1) != '/')
    $path_to_script = $path_to_script.'/';

// We create our own request URI with the path removed and only the parts to rewrite included
$request_uri = substr(urldecode($_SERVER['REQUEST_URI']), strlen($path_to_script));
if (strpos($request_uri, '?') !== false)
    $request_uri = substr($request_uri, 0, strpos($request_uri, '?'));

$rewritten_url = '';
$url_parts = array();
// We go through every rewrite rule
foreach ($forum_rewrite_rules as $rule => $rewrite_to)
{
    // We have a match!
    if (preg_match($rule, $request_uri))
    {
        $rewritten_url = preg_replace($rule, $rewrite_to, $request_uri);
        $url_parts = explode('?', $rewritten_url);

        // If there is a query string
        if (isset($url_parts[1]))
        {
            $query_string = explode('&', $url_parts[1]);

            // Set $_GET properly for all of the variables
            // We also set $_REQUEST if it's not already set
            foreach ($query_string as $cur_param)
            {
                $param_data = explode('=', $cur_param);

                // Sometimes, parameters don't set a value (eg: script.php?foo), so we set them to null
                $param_data[1] = isset($param_data[1]) ? $param_data[1] : null;

                // We don't want to be overwriting values in $_REQUEST that were set in POST or COOKIE
                if (!isset($_POST[$param_data[0]]) && !isset($_COOKIE[$param_data[0]]))
                    $_REQUEST[$param_data[0]] = urldecode($param_data[1]);

                $_GET[$param_data[0]] = urldecode($param_data[1]);
            }
        }
        break;
    }
}

// If we don't know what to rewrite to, we show a bad request messsage
if (empty($rewritten_url))
{
    define('FORUM_HTTP_RESPONSE_CODE_SET', 1);
    header('HTTP/1.1 404 Not Found');

    // Allow an extension to override the "Bad request" message with a custom 404 page
    ($hook = get_hook('re_page_not_found')) ? eval($hook) : null;

    error('Page Not found (Error 404):<br />The requested page <em>'.forum_htmlencode($request_uri).'</em> could not be found.');
}

// We change $_SERVER['PHP_SELF'] so that it reflects the file we're actually loading
$_SERVER['PHP_SELF'] = str_replace('rewrite.php', $url_parts[0], $_SERVER['PHP_SELF']);

require FORUM_ROOT.$url_parts[0];

Let's say i want to redirect the "blabla" (mydomain.com/blabla) url to mydomain.com/topicxx-my-subject.html

Where can i put this to work ?

I've tried

RewriteCond %{QUERY_STRING} ^blabla$
RewriteRule ^index\.php$ [url]http://mydomain.com/topicxx-my-subject.html[/url]? [L,R=301] 

and

RewriteCond %{QUERY_STRING} ^blabla$
RewriteRule ^rewrite\.php$ [url]http://mydomain.com/topicxx-my-subject.html[/url]? [L,R=301] 

but it doesn't work (when i try mydomain.com/blabla i get a 404 error, which normally redirect with my htaccess blog)

I tried

Redirect 301 /blabla http://mydomain.com/topicxx-my-subject.html

or 

RedirectPermanent /blabla http://mydomain.com/topicxx-my-subject.html

Same problem, a 404 error.

What should i write, in which file?
Thanks,

Pierre

2 (edited by zetrader 2019-02-12 12:38)

Re: Redirect 301 in htaccess with url using schema file based (fancy)

Nobody knows?

EDIT : in htaccess

Redirect 301 /myfolder/blabla http://mydomain.com/myfolder/topicxx-this-my-subject.html

If i write blabla in the url of my forum, it makes the correct redirection.

But if it's :

Redirect 301 /myfolder/?articlexx http://mydomain.com/myfolder/topicxx-this-my-subject.html

the ?articlexx url goes to index, the problem seems to bee with the ? in the url.

If it's :

Redirect 301 /myfolder/articlexx http://mydomain.com/myfolder/topicxx-this-my-subject.html

the articlexx makes the correct redirection.

Re: Redirect 301 in htaccess with url using schema file based (fancy)

@Zetrader, use php code for redirection.

For example:

$link = 'your_new_site');

header("HTTP/1.1 301 Moved Permanently");
header("Location: " . $link);
exit();