Search code examples
php.htaccessmod-rewritehttp-redirecthttp-status-code-301

How to 301 redirect URL with PHP parameters to root DIR in .htaccess?


I have searched how to redirect url with parameters to root dir but I can't get it to work :(

This is what the .htaccess code looks like:

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

RewriteEngine On
RewriteCond %{QUERY_STRING} id=1
RewriteRule ^phpnuke/modules\.php$ /? [L,R=301]

I have url such as "http://www.domain.com/phpnuke/modules.php?name=News&file=article&sid=60" and I want to redirect it to "http://www.diagonally.org/"


Solution

  • Keep redirect rule before default WP rewrite rule and match correct query string:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{QUERY_STRING} name=News&file=article&sid=60
    RewriteRule ^phpnuke/modules\.php$ /? [L,R=301]
    
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>