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.

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

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致