1

I'm trying to make clean urls with php and im getting some errors. Hope someone can help.

My .htaccess is as follows:

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
# this is the initialization
# For security reasons, Option followsymlinks cannot be overridden.
#Options         +FollowSymLinks
Options +SymLinksIfOwnerMatch
RewriteEngine   On
RewriteBase     /
# these are the rewrite conditions
RewriteCond     %{REQUEST_FILENAME}     !-f
RewriteCond     %{REQUEST_FILENAME}     !-d
# and finally, the rewrite rules
RewriteRule     ^([a-zA-Z0-9\-]+)/?$    /price/index.php?var1=$1 [L,QSA]
RewriteRule     ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$   /price/index.php?var1=$1&var2=$2 [L,QSA]
RewriteRule     ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$    /price/index.php?var1=$1&var2=$2&var3=$3 [L,QSA]

Actually I'm trying to make clean urls in the subfolder price/.

When I enter: mydomain.com/price/param1 it works perfect and it actually redirects me to mydomain.com/price/index.php?var1=param1

But when I try to go forward with more variables is when I get the problem. When I try to access mydomain.com/price/param1/param2 I'm not redirected and a 404 error appears.

Any idea? Thanks in advance.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
Dan Stern
  • 2,155
  • 8
  • 26
  • 38
  • 1
    Where this .htaccess is located -- in website root folder or is it /price/.htaccess ? – LazyOne Jul 14 '11 at 20:24
  • 2
    The two `RewriteCond` only apply to the single rule immediately following them. – Kevin Stricker Jul 14 '11 at 20:25
  • the .htacces is located in the root folder, but thats not the problem. mootinator, what that means? – Dan Stern Jul 14 '11 at 20:30
  • 1
    @Dan Stern: It means the way you have the .htaccess code above, if you visited `/price/var1/var2` and there were some directories hierarchically the same way as your url, then the rewrite would kick in, and you wouldn't be able to access the directories. That's why the conditions are there, `!-f` and `!-d` means, rewrite only if there doesn't exist a file or a directory at the same location with the request. – Shef Jul 14 '11 at 20:38

1 Answers1

4

Try:

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php

<IfModule mod_rewrite.c>
    # This is the initialization
    # For security reasons, Option followsymlinks cannot be overridden.
    #Options +FollowSymLinks
    Options +SymLinksIfOwnerMatch
    RewriteEngine On
    RewriteBase /

    # /price/var1/var2/var3
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^price/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ /price/index.php?var1=$1&var2=$2&var3=$3 [L,QSA]

    # /price/var1/var2
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^price/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ /price/index.php?var1=$1&var2=$2 [L,QSA]

    # /price/var1
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^price/([a-zA-Z0-9\-]+)/?$ /price/index.php?var1=$1 [L,QSA]
</IfModule>
Shef
  • 44,808
  • 15
  • 79
  • 90