'.htaccess RewriteRule Weird Bug

# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working

Options +FollowSymLinks
RewriteEngine On

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

# Redirect www to non-www
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

#--Redirect from "/?page=foo" to "/page/foo"--#
RewriteCond %{THE_REQUEST} /\?page=([^\s]+) [NC]
RewriteRule ^ /page/%1? [NC,L,R]
#--Rewrite "/page/foo/" to "/?page=foo"--#
RewriteRule ^page/([^/]+)/?$ /?page=$1 [NC,L,QSA]

#--Redirect from "/search.php?search=foo" to "/search/foo"--#
RewriteCond %{THE_REQUEST} /search\.php\?search=([^\s]+) [NC]
RewriteRule ^ /search/%1? [NC,L,R]
#--Rewrite "/search/foo/" to "/search.php?search=foo"--#
RewriteRule ^search/([^/]+)/?$ /search.php?search=$1 [NC,L,QSA]

#--Redirect from "/search.php?search=foo&page=bar" to "/search/foo/page/bar"--#
RewriteCond %{THE_REQUEST} /search\.php\?search=([^&]+)&page=([^\s]+) [NC]
RewriteRule ^ /search/%1/page/%2? [NC,L,R]
#--Rewrite "/search/foo/page/bar" to "/search.php?search=foo&page=bar"--#
RewriteRule ^search/([^/]+)/page/([^/]+)/?$ /search.php?search=$1&page=$2 [NC,L,QSA]

#--Redirect from "/post.php?id_post=foo" to "/post/foo"--#
RewriteCond %{THE_REQUEST} /post\.php\?id_post=([^\s]+) [NC]
RewriteRule ^ /post/%1? [NC,L,R]
#--Rewrite "/post/foo/" to "/post.php?id_post=foo"--#
RewriteRule ^post/([^/]+)/?$ /post.php?id_post=$1 [NC,L,QSA]

# Support new SEO-friendly URLs
RewriteRule page/(.*) ?page=$1
RewriteRule search/(.*) search.php?search=$1
RewriteRule search/(.*)/page/(.*) search.php?search=$1&page=$2
RewriteRule post/(.*) post.php?id_post=$1

This is my file and its working fine but for some reason this one:

#--Redirect from "/search.php?search=foo&page=bar" to "/search/foo/page/bar"--#
RewriteCond %{THE_REQUEST} /search\.php\?search=([^&]+)&page=([^\s]+) [NC]
RewriteRule ^ /search/%1/page/%2? [NC,L,R]
#--Rewrite "/search/foo/page/bar" to "/search.php?search=foo&page=bar"--#
RewriteRule ^search/([^/]+)/page/([^/]+)/?$ /search.php?search=$1&page=$2 [NC,L,QSA]

Its not working like it should, it would make this link:

search.php?search=$1&page=$2

into

search/search/$1/page/$2

but the result is this one:

search/search/$1&page=$2


Solution 1:[1]

Your rules are not in the right order. A rule like this:

RewriteCond %{THE_REQUEST} /search\.php\?search=([^\s]+) [NC]
RewriteRule ^ /search/%1? [NC,L,R]

will match requests like /search.php?search=blah as well as /search.php?search=blah&page=foo. Because there's nothing that stops the end of the match in the condition. So you need to put the more detailed match before the more general one. This goes the same with all your ?page=XXX rules as well.

Also, your routing rule:

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

Needs to be at the very end, and not before and rules that redirect.

So:

# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working

Options +FollowSymLinks
RewriteEngine On

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

# Redirect www to non-www
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

#--Redirect from "/?page=foo" to "/page/foo"--#
RewriteCond %{THE_REQUEST} /\?page=([^\s]+) [NC]
RewriteRule ^ /page/%1? [NC,L,R]
#--Rewrite "/page/foo/" to "/?page=foo"--#
RewriteRule ^page/([^/]+)/?$ /?page=$1 [NC,L,QSA]

#--Redirect from "/search.php?search=foo&page=bar" to "/search/foo/page/bar"--#
RewriteCond %{THE_REQUEST} /search\.php\?search=([^&]+)&page=([^\s]+) [NC]
RewriteRule ^ /search/%1/page/%2? [NC,L,R]
#--Rewrite "/search/foo/page/bar" to "/search.php?search=foo&page=bar"--#
RewriteRule ^search/([^/]+)/page/([^/]+)/?$ /search.php?search=$1&page=$2 [NC,L,QSA]

#--Redirect from "/search.php?search=foo" to "/search/foo"--#
RewriteCond %{THE_REQUEST} /search\.php\?search=([^\s]+) [NC]
RewriteRule ^ /search/%1? [NC,L,R]
#--Rewrite "/search/foo/" to "/search.php?search=foo"--#
RewriteRule ^search/([^/]+)/?$ /search.php?search=$1 [NC,L,QSA]

#--Redirect from "/post.php?id_post=foo" to "/post/foo"--#
RewriteCond %{THE_REQUEST} /post\.php\?id_post=([^\s]+) [NC]
RewriteRule ^ /post/%1? [NC,L,R]
#--Rewrite "/post/foo/" to "/post.php?id_post=foo"--#
RewriteRule ^post/([^/]+)/?$ /post.php?id_post=$1 [NC,L,QSA]

# Support new SEO-friendly URLs
RewriteRule page/(.*) ?page=$1
RewriteRule search/(.*) search.php?search=$1
RewriteRule search/(.*)/page/(.*) search.php?search=$1&page=$2
RewriteRule post/(.*) post.php?id_post=$1

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jon Lin