简体   繁体   English

htacces url重写条件

[英]htacces url rewriting with rewrite condition

Currently, i have system which works through htaccess like need to hardcore url everytime a new product is added to the system, something like below 目前,我有一个可以通过htaccess运行的系统,就像每次向系统中添加新产品时都需要使用硬核url一样

RewriteRule ^18th-and-19th-century$ sub_category.php?id=18th-and-19th-century
RewriteRule ^20th-century-furniture$ product.php?id=20th-century-furniture
RewriteRule ^pair-lacquer-cabints$ product_info.php?id=pair-lacquer-cabints

and also there are few static pages as, 而且静态页面很少,

RewriteRule aboutus aboutus.php? 
RewriteRule contactus contactus.php? 

the Idea here, what i want is to redirect the product info, category info to some index.php and access the static page as they are, 这里的想法,我想要的是将产品信息,类别信息重定向到一些index.php并按原样访问静态页面,

RewriteRule aboutus$ aboutus.php?
RewriteRule contactus contactus.php?
RewriteRule ^ index.php? 

i tried this but everytime it takes me too the index.php page,i think there needs to some condition thing here but not able to understand how to go forward 我尝试了这个,但是每当它也需要我index.php页面时,我认为这里需要一些条件,但无法理解该如何进行

Thanks in Advance 提前致谢

Try 尝试

RewriteEngine On
RewriteRule ^sub_category/([^/]*)$ /sub_category.php?id=$1 [L]
RewriteRule ^product/([^/]*)$ /product.php?id=$1 [L]
RewriteRule ^product_info/([^/]*)$ /product_info.php?id=$1 [L]

Output URLs will be 输出网址为

http://example.com/sub_category/18th-and-19th-century
http://example.com/product/20th-century-furniture
http://example.com/product_info/pair-lacquer-cabints

You have wrote the all url at same directory level, so there is no way you can write dynamic .htaccess rule. 您已在同一目录级别上编写了所有url,因此无法编写动态.htaccess规则。 You need to add some identifier or some pattern in all different kind of url's which are going to access different php page. 您需要在所有要访问不同php页面的不同类型的url中添加一些标识符或某种模式。

RewriteRule ^18th-and-19th-century$ sub_category.php?id=18th-and-19th-century
RewriteRule ^20th-century-furniture$ product.php?id=20th-century-furniture
RewriteRule ^pair-lacquer-cabints$ product_info.php?id=pair-lacquer-cabints
RewriteRule aboutus aboutus.php

Like in above pages you can use url's in following style 像以上页面一样,您可以使用以下样式的url

Subcategory URL: www.domain.com/18th-and-19th-century
Product URL: www.domain.com/18th-and-19th-century/list or www.domain.com/18th-and-19th-century/20th-century-furniture
Product info: www.domain.com/18th-and-19th-century/20th-century-furniture/pair-lacquer-cabints

OR 要么

Subcategory URL: www.domain.com/category/18th-and-19th-century
Product URL: www.domain.com/product/20th-century-furniture
Product info: www.domain.com/product_info/pair-lacquer-cabints

So accordingly you can define levels and write rewrite rules. 因此,您可以定义级别并编写重写规则。

Similarly you can write URL for static pages 同样,您可以为静态页面写URL

www.domain.com/content/aboutus
www.domain.com/content/contactus

or www.domain.com/aboutus [if you don't use first level URL for subcategory like www.domain.com/18th-and-19th-century then you can use for static pages] www.domain.com/aboutus [如果您不对诸如www.domain.com/18th-and-19th-century这样的子类别使用第一级URL,则可以用于静态页面]

if this answer is helpful to you then i will write .htaccess rules 如果这个答案对您有所帮助,那么我将编写.htaccess规则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM