Using several rewrite rules .htaccess
I have a website wich is based on a searchpage, which shows a list, and next is a detail page of the items.
if visitors use rootdomain.com/searchterm
, should be redirected to rootdomain.com/index.php?keyword=searchterm
.
I had that working now for the details rootdomain.com/detail/productname
, should be redirected to rootdomain.com/detail.php?name=productname
and here I fail...
I keep getting redirected to index.php with my $1 as 'detail/productname'
this is my .htacces
so far:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/detail/(.*)$ detail.php?name=$1 [L,QSA] -> L here doesn't work either.
RewriteRule ^(.*)$ index.php?keyword=$1 [L,QSA]
Update
Ok, so I am very close now
Problem was that the rewrite condition only is valid for the next rule so I have to repeat that apparantly, I also added ignore detail.php in the first condition, because my redirect on detail got redirected again to index. now for some reason my condition for -f doesn't seem to work on detail/ all my css and images are not working. what is best way to exclude them
RewriteEngine on
RewriteCond $1 !^(index\.php|detail\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^detail/(.*)$ detail.php?name=$1 [L,QSA]
RewriteCond $1 !^(index\.php|detail\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?keyword=$1 [L,QSA]
Update 2
So I got it to work.. But I guess this is not the shortest way to do it. So if anyone is interested feel free to make this shorter.
RewriteEngine on
RewriteCond $1 !^(index\.php|detail\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^detail/(images|css|js)/(.*)$ $1/$2 [L,QSA]
RewriteCond $1 !^(index\.php|detail\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^detail/(.*)$ detail.php?name=$1 [L,QSA]
RewriteCond $1 !^(index\.php|detail\.php|resources|robots\.txt|\.png)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?keyword=$1 [L,QSA]
You need to add the L
('last') flag to the first RewriteRule
:
RewriteRule ^/detail/(.*)$ detail.php?name=$2 [L,QSA]
According to the documentation, this flag will:
Stop the rewriting process here and don't apply any more rewrite rules. This corresponds to the Perl last command or the break command in C. Use this flag to prevent the currently rewritten URL from being rewritten further by following rules.
Without it, Apache continues to apply the second rewrite rule after matching the first. Because the second rule affects everything, it overrides the first one. (Another solution would be to write your patterns so that no two URLs match the same pattern, for example by changing the second pattern to ^[^/](.*)$
.)