drfu80954 2010-07-28 19:19
浏览 39
已采纳

尝试将产品名称和产品ID放入url,最后使用斜杠,而不使用?p =

I guess the title almost says it all.

My original url looks like: http://www.something.com/buy/index.php?p=025823

The .htaccess I'm using looks like this:

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(\d+)*$ ./index.php?p=$1

The resulting url looks like: http://www.something.com/buy/025823

I'd like to end up with a url like: http://www.something.com/buy/Product Name/025823/

Can anyone help? Especially that Gumbo fella! LOL

  • 写回答

1条回答 默认 最新

  • dstm2014 2010-07-28 19:41
    关注

    If you can control the links that are generated to your /buy/ pages, just insert the product name in the URL (keeping in mind the rules of URLs, like no spaces allowed. Usually, people substitute a - for spaces). Then modify your rewrite regex to ignore the product name portion of your URL.

    RewriteBase /
    RewriteRule ^/buy/(.+)/(\d+)/$ ./index.php?p=$2
    

    If you can't control the links, things get more complicated since you'll have to look up the product name using a RewriteMap and then perform a redirect. I just recently covered this is in a similar question. My answer to that question goes in to greater detail about using RewriteMap, but I don't think the details of that answer apply to your situation since using a statically-defined list of products probably won't meet your needs.

    Basically, you'll need to implement a script that can be called from mod_rewrite's RewriteMap rule. You'll need something like:

    RewriteMap prodname pgm:/path/to/script/namelookup.sh
    RewriteRule ^/buy/(\d+)/$ /buy/${prodname:$1}/$1/ [R=permanent] 
    

    Where namelookup.sh (or whatever) returns the product's name (on stdout) in a URL-encoded format. You'll definitely want to take a look at mod_rewrite's documentation about using RewriteMap, specifically the last section of the RewriteMap Directive on using an external rewriting program.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?