2

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]

2 Answers 2

1

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

1
  • I had thought of that before, but that didn't work either. So there must be another problem. any other suggestions?
    – woony
    Mar 21, 2013 at 20:25
0

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/...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .