douzang7928 2013-09-26 13:45 采纳率: 100%
浏览 46
已采纳

使用HTACCESS重写动态URL

I've been reading a few tutorials online and searched a couple of other questions here but I cant seem to grasp the re-writing of dynamic URLS using the HTACCESS file.

I have URLs like:

products.php?cat=2
products.php?cat=3
products.php?cat=4

I would like them to just say products/2 for example

equipment.php?cat=2&subCat=1
equipment.php?cat=2&subCat=2
equipment.php?cat=2&subCat=3

I would like these to say equipment/2/1 for example

product.php?id=3010-Z89CH24

I would like these to say product/3010-Z89CH24 for example

but so far just trying to change the products.php pages I have this:

    RewriteEngine On
RewriteCond %{HTTP_HOST} ^*******\.com [NC]
RewriteRule (.*) http://www.*******.com/$1 [L,R=301]

Options +FollowSymLinks

RewriteRule ^products/'^([1-9][0-9]{0,2})'$ http://www.*******.com/products.php?cat=/$2 [L] 
# REMOVE PHP EXTENSIONS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

the thing is I dont think the end part "/$2" is correct and im unsure of the rest of it too :/

Either way its not working for me and I am stuck and struggling to understand it :(

can someone help me please?

  • 写回答

3条回答 默认 最新

  • dqq9695 2013-09-26 13:59
    关注

    Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    ## If the request is for a valid directory
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    ## If the request is for a valid file
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    ## If the request is for a valid link
    RewriteCond %{REQUEST_FILENAME} -l
    ## don't do anything
    RewriteRule ^ - [L]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?cat=([^\s&]+)&subcat=([^\s&]+)\s [NC]
    RewriteRule ^ /%1/%2/%3? [R=301,L]
    
    RewriteRule ^([^/.]+)/([^/]+)/([^/]+)/?$ /$1.php?cat=$2&subcat=$3 [L,QSA]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?cat=([^\s&]+)\s [NC]
    RewriteRule ^ /%1/%2? [R=301,L]
    
    RewriteRule ^([^/.]+)/([^/]+)/?$ /$1.php?cat=$2 [L,QSA]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改