0

I came across some htaccess url rewrite rules which are conflicting to existing url.

  1. To write my category pages to www.mydomain.com/categoryname.html

    I'm using following rule which is working fine but all my other pages like www.mydomain.com/about.html shows 404 not found

    RewriteRule ^([^/]*)\.html$ /category_parent.php?ctg=$1 [L]
    
  2. To write my product pages like www.mydomain.com/25/productname.html

    I'm using following code which is also working fine

    RewriteRule ^([^/]*)/([^/]*)\.html$ /product.php?id=$1&url=$2 [L]
    

    but the above rewrite rule conflicting with sub category pages for which I'm using following code:

    RewriteRule ^([^/]*)/([^/]*)\.html$ /category_child.php?pctg=$1&ctg=$2 [L]
    

In brief, I want to write to,

www.mydomain.com/categoryname.html

www.mydomain.com/categoryname/subcategoryname.html

www.mydomain.com/25/productname.html

But Some rules messing with each other. I'll appreciate if you can provide me little clue on this.

4

1 回答 1

0

If your files are colliding the non-existent to existent ones, you should use a condition to verify if a file, directory or symbolic link exists.

For files:

RewriteCond %{REQUEST_FILENAME} !-f

For folders:

RewriteCond %{REQUEST_FILENAME} !-d

For symbolic links:

RewriteCond %{REQUEST_FILENAME} !-l

The exclamation mark ! is used to negate their meaning !-f means if the file does not exist then apply rule if it exists then do not apply rule.

Keep in mind that a RewriteCond should be followed by a RewriteRule and you can have one or more conditions per RewriteRule, for example:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*)\.html$ /category_parent.php?ctg=$1 [L]

In this case it means if directory does not exist and file does not exist and symbolic link does not exist then we redirect but if any of the previous condition exist then we don't redirect.

于 2013-08-13T07:02:17.093 回答