Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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]
share|improve this question

2 Answers 2

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 ^[^/](.*)$.)

share|improve this answer
    
I had thought of that before, but that didn't work either. So there must be another problem. any other suggestions? –  woony Mar 21 '13 at 20:25

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.

What happened, first version, the redirect from detail/something worked, but got then redirected to index.php?keyword=detail.php?name=something so i had to exclude detail.php in the first condition line. Also rewriteCond are valid for the next rule, not for all. so I had to repeat them.

Lastly to have my images and css work i had to rewrite them to the base url for the 3 folders i have images/css/js in .

If this can be made shorter or more sufficient, feel free to do so

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]

--edit it still doesn't seem fool proof, when using urlencoded parts behind detail/...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.