PHP .htaccess modification / extension with additional string matching
I have a .htaccess file which i use in a Codeigniter Project :
DirectoryIndex index.php
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Currently everything works fine and the rewrite to the controller works as well. E.g:
www.abc.com/login --> www.abc.com/index.php?/login
What i want to accomplish is that an additional string (kind of security code) is matched by the htaccess and only then the rewrite takes place:
www.abc.com/abcdefgh/login --> www.abc.com/index.php?/login
I tried all of the following but it just won't work:
RewriteRule ^abcdefgh/(.*)$ index.php?/$1 [L,QSA]
RewriteRule ^/abcdefgh/(.*)$ index.php?/$1 [L,QSA]
RewriteRule ^abcdefgh\/(.*)$ index.php?/$1 [L,QSA]
RewriteRule ^\/abcdefgh\/(.*)$ index.php?/$1 [L,QSA]
Does anybody have an idea why?
Answer
Solution:
first matching rule wins. place your new rules on top of your RewriteRule
block.
it seems like RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
is matched still before you newly added rules.
Answer
Solution:
Maybe you could thread that "security code" in PHP ?
URL : www.abc.com/login/abcdefgh
{-code-2}
Answer
Solution:
in htaccess you don't have the / in the beginning of the url path
following config works for me
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^abcdefgh/(.*)$ index.php/$1 [L,QSA]
in php you use the $_SERVER['PATH_INFO'] to get the $1 part, and the query string stays in tact, e.g.
http://127.0.0.1/a/b/abcdefgh/a/b/c.php?foo=bar
//calls /var/www/a/b/index.php
$_SERVER["PATH_INFO"] => '/a/b/c.php'
$_SERVER["QUERY_STRING"] => 'foo=bar'
$_GET["foo"] => "bar"
Source