Search code examples
regexapachewordpress.htaccesshttp-status-code-301

Dynamic 301 Redirect Fighting with Wordpress


This is a followup to another question I asked earlier (Earlier redirect question). My root .htaccess has several rules in this format:

RewriteCond %{REQUEST_URI} ^/leadership/detail/$
RewriteCond %{QUERY_STRING} q=([0-9]+)$ 
RewriteRule ^(.*)$ /$1?p=%1 [L,R=301]

But when I tried to add this:

RewriteCond %{REQUEST_URI} ^/blog/detail/$
RewriteCond %{QUERY_STRING} q=([0-9]+)$ 
RewriteRule ^(.*)$ /$1?p=%1 [L,R=301]

It didn't work. /blog/ is a Wordpress install and I'm thinking in it's conflicting with the .htaccess file that's in /blog/ that has the following:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress

Where can I insert (or edit) code snippet #2 in the /blog/.htaccess file to make it work properly?


Solution

  • Have your combined .htaccess like this:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} ^/blog/detail/$
    RewriteCond %{QUERY_STRING} q=([0-9]+)$ [NC]
    RewriteRule ^(.*)$ /blog/$1?p=%1 [L,R=301]
    
    RewriteBase /blog/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{QUERY_STRING} !^p= [NC]
    RewriteRule . /blog/index.php [L]
    </IfModule>
    
    # END WordPress