there is http: // siti / news /? PAGEN_1 = 1 # nav_start how to make 301 on http: // siti / news / ?

    3 answers 3

    Anchors are not sent to the server as part of the request.

    Option using htaccess (without anchor):

    RewriteCond %{REQUEST_URI} ^/blog/news/$ RewriteCond %{QUERY_STRING} ^PAGEN_1=1$ RewriteRule ^(.*)$ /news/? [R=301,L] 

    Option on php (without anchor):

     if ($_SERVER['REQUEST_URI'] === '/news/?PAGEN_1=1') { header("HTTP/1.1 301 Moved Permanently"); header("Location: /news/"); exit(); } 

    Make a redirect using .htaccess or php , taking into account the anchor will not work.
    However, this can be done using javascript .
    But even here there is a catch: it will not work to send the 301st header , since it is generated on the server.

    Javascript code :

     <script type="text/javascript"> function parse_url(str, component) { // http://kevin.vanzonneveld.net // + original by: Steven Levithan ( http://blog.stevenlevithan.com ) // + reimplemented by: Brett Zamir ( http://brett-zamir.me ) // + input by: Lorenzo Pisani // + input by: Tony // + improved by: Brett Zamir ( http://brett-zamir.me ) // % note: Based on http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js // % note: blog post at http://blog.stevenlevithan.com/archives/parseuri // % note: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js // % note: Does not replace invalid characters with '_' as in PHP, nor does it return false with // % note: a seriously malformed URL. // % note: Besides function name, is essentially the same as parseUri as well as our allowing // % note: an extra slash after the scheme/protocol (to allow file:/// as in PHP) // * example 1: parse_url('http://username:password@hostname/path?arg=value#anchor'); // * returns 1: {scheme: 'http', host: 'hostname', user: 'username', pass: 'password', path: '/path', query: 'arg=value', fragment: 'anchor'} var query, key = ['source', 'scheme', 'authority', 'userInfo', 'user', 'pass', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'fragment'], ini = (this.php_js && this.php_js.ini) || {}, mode = (ini['phpjs.parse_url.mode'] && ini['phpjs.parse_url.mode'].local_value) || 'php', parser = { php: /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-scheme to catch file:/// (should restrict this) }; var m = parser[mode].exec(str), uri = {}, i = 14; while (i--) { if (m[i]) { uri[key[i]] = m[i]; } } if (component) { return uri[component.replace('PHP_URL_', '').toLowerCase()]; } if (mode !== 'php') { var name = (ini['phpjs.parse_url.queryKey'] && ini['phpjs.parse_url.queryKey'].local_value) || 'queryKey'; parser = /(?:^|&)([^&=]*)=?([^&]*)/g; uri[name] = {}; query = uri[key[12]] || ''; query.replace(parser, function($0, $1, $2) { if ($1) { uri[name][$1] = $2; } }); } delete uri.source; return uri; } url = parse_url(window.location); if (url.path === '/news/' && url.query === 'PAGEN_1=1' && url.fragment === 'nav_start') { window.location.replace('/news/'); } </script> 

    If the 301st heading is necessary, then the anchor will have to be dropped.
    If you need something more complicated, then to work with the url there is an excellent javascript library: URI

      Look what is meant:

      That's either (if you need exactly this):

       RewriteEngine On RewriteRule ^news/$ /news/?PAGEN_1=1#nav_start [L,R=301] 

      But I think that the goal is meant precisely this - statically through mod_alias

       Redirect 301 /news/?PAGEN_1=1#nav_start http://siti/news/ 
      • does not plow, #nav_start # = comment (replaced by \% 23) the same zero result .. - kotdg
      • I don’t have a web server at hand to test, but I’m confused about 2 things: 1. Link contains an anchor (anchor) 2. Both URL refer to what is written in DirectoryIndex - i.e. on the same page. I'll try to check later. - void
        Redirect 301 

      But it is better to cut the URL in the engine, why do it in htaccess?

        $url = current(explode('?', $_SERVER['REQUEST_URI']));