htaccess use flag N for next condition

Some searchengines have listed urls in their index with two errors in it. I want to redirect them to the correct location. My problem is, I want to combine two RewriteRules, so it comes only to one single redirect-request.

The misspelled Request-path looks like this:

. /subdir/ page.php?id=0 & amp; name=text

I’ve marked the problem with bold formating. At first I want to Rewrite the & amp; to the single char & and in the second step, I want to rewrite the subdir to the root directory.

I’ve this rules in my htacces:

RewriteCond %{THE_REQUEST} s(.*?)&([^s]*) [NC]
RewriteRule ^ /%1&%2 [N,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^feeds/print.php(.*) /print.php$1 [L,NC,R=301]

The second condition for the subdir is working fine already, but the first one makes problems! I want to use the N-Flag to use the replaced request for the next condition. It seems that the server is running into an infinite looping or something similar. If I use L in replace for N its working, but makes two redirects.

Source: .htaccess

1 Comment

  1. Digin Dominic

    You don’t need N since you’re doing full redirect using R flag. 2nd rule will be executed anyway after first one removes &.

    Also your regex needs some tweaks. Try this code:

    RewriteCond %{THE_REQUEST} s/+(.*?)&(S*)s [NC]
    RewriteRule ^ /%1&%2 [L,NE,R=302]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^feeds/print.php(.*)$ /print.php$1 [L,NC,R=301]


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.