• My mod_rewrites were working well until a few months ago, when they seemed to spontaneously break. ;(

    https://mysite.com/musica.php?item=54 displays the page as I would like.

    I’d like to be able to enter https://mysite.com/musica.php/media/test/54 and get the same page. However, when I use the following mod_rewrite, I get a 404 error:

    RewriteRule ^musica.php/media/([^/\.]+)/([^/\.]+) /musica.php?item=$2 [QSA,L]

    When I do a 301 redirect instead, it works fine, suggesting it’s not a problem with the regular expression:

    RewriteRule ^musica.php/media/([^/\.]+)/([^/\.]+) /musica.php?item=$2 [R=301,L]

    Any ideas? For the sake of completeness, here’s the code in my functions.php I’m using to generate these rewrites:

    add_action('generate_rewrite_rules', 'wp_insertMyRewriteRules');
    add_action('admin_init', 'my_flush_rules');
    
    function my_flush_rules(){
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
    
    function wp_insertMyRewriteRules($rules) {
            global $wp_rewrite;
    	$newrules = array();
    	$newrules['musica.php/media/([^/\.]+)/([^/\.]+)'] = 'musica.php?item=$2';
            $wp_rewrite->non_wp_rules = $newrules + $wp_rewrite->non_wp_rules;
    }

    and here’s an abbreviated copy of my .htaccess file:

    <IfModule mod_rewrite.c>
    RewriteRule ^category/(.+)$ https://mysite.com/ [R=301,L]
    RewriteRule ^tag/(.+)$ https://mysite.com/ [R=301,L]
    RewriteRule ^postings$ https://mysite.com/ [R=301,L]
    </IfModule>
    
    # BEGIN W3TC Page Cache core
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{HTTP:Accept-Encoding} gzip
        RewriteRule .* - [E=W3TC_ENC:_gzip]
        RewriteCond %{REQUEST_METHOD} !=POST
        RewriteCond %{QUERY_STRING} =""
        RewriteCond %{HTTP_COOKIE} !(comment_author|wp-postpass|w3tc_logged_out|wordpress_logged_in) [NC]
        RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index.html%{ENV:W3TC_ENC}" -f
        RewriteRule .* "/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index.html%{ENV:W3TC_ENC}" [L]
    </IfModule>
    # END W3TC Page Cache core
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteRule ^musica.php/media/([^/\.]+)/([^/\.]+) /musica.php?item=$2 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    
    IndexIgnore *

    Thanks so much for your help!

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘mod_rewrite frustration’ is closed to new replies.