简体   繁体   English

删除多个参数并重写url Htaccess

[英]Remove multiple parameters and rewrite url Htaccess

I am trying to rewrite the following Url for SEO and userfriendly purposes using htaccess or other solution. 我正在尝试使用htaccess或其他解决方案重写以下Url用于SEO和用户友好目的。 I have searched and found many answers but none gives me multiple parameters and i am not advance at this. 我已经搜索并找到了很多答案,但没有一个给我多个参数,我没有在这方面取得进展。

mysite.com/myfile.php?dept=1&prodid=161&user=2018&ulevel=1&parentcust=1&prodname=My widget

to

mysite.com/products/mywidget

if My Widget has spaces or apostrophes i need them removed. 如果我的小工具有空格或撇号我需要删除它们。 example the product Tiger's eye widget should be tigerseyewidget or tigers-eye-widget 例如, Tiger's eye widget应该是tigerseyewidgettigers-eye-widget

You don't have to solve your problem with rewrites only. 您不必仅使用重写来解决问题。

For starters, let's strip your query string: 首先,让我们去掉你的查询字符串:

  • user id & user level do not belong there - it's dangerous to have these in your query string. 用户ID和用户级别不属于那里 - 在查询字符串中包含这些内容很危险。 What if I change it in my browser to user=1&ulevel=5 - am I then suddenly the administrator with access to all area's? 如果我在浏览器中将其更改为user = 1&ulevel = 5,那么我是否突然管理员可以访问所有区域? Instead, work with sessions and store the user id & ulevel there 相反,使用会话并在那里存储用户ID和ulevel
  • Do you really need the product id as well as the product name in the query string? 您是否真的需要产品ID以及查询字符串中的产品名称? I'd say: drop the product name, the id should be enough information for you to retrieve the product name. 我会说:删除产品名称,id应该是足够的信息,以便您检索产品名称。
  • parentcust: what is that? 亲子:这是什么? Do you really need it in the query string? 你真的需要在查询字符串中吗? What if I change the value? 如果我改变了价值怎么办? Looks like something that belongs in a session as well. 看起来像属于会话的东西。

After stripping that, all you're left with is myfile.php?dept=1&prodid=161 - now that's something that's much easier to rewrite! 在剥离之后,你剩下的就是myfile.php?dept = 1&prodid = 161 - 现在这是更容易重写的东西!

How to rewrite is another question. 如何改写是另一个问题。 There are plenty examples on SO how to rewrite a query string based request to a "pretty url". 有很多关于如何将基于查询字符串的请求重写为“漂亮网址”的示例。 Changing Tiger's eye widget to tigers-eye-widget is also well documented. Tiger's eye widget改为tigers-eye-widget也有很好的记录。

But.. who generates these links? 但是..谁生成这些链接? If you generate these links, why not turn it around and link your products to eg mysite.com/tigers-eye-widget.html and then in PHP map "tigers-eye-widget.html" to product id 161 & dept 1? 如果您生成这些链接,为什么不转过它并将您的产品链接到例如mysite.com/tigers-eye-widget.html,然后在PHP中将“tigers-eye-widget.html”映射到产品ID 161和dept 1?

Your problem should really only apply to legacy URL's that people may have bookmarked or still appear in search results. 您的问题应该仅适用于人们可能已添加书签或仍会显示在搜索结果中的旧版网址。 But for those cases it's perfectly acceptable to redirect them instead of rewriting them. 但对于那些情况,重定向它们而不是重写它们是完全可以接受的。 So you can keep the mapping in a database or textfile, process the request with PHP and do a 301 redirect to the new URL - that way you tell search engines to replace the old URL with the new one and you don't have to worry about complicated multiple .htaccess rewrite rules. 因此,您可以将映射保留在数据库或文本文件中,使用PHP处理请求并执行301重定向到新URL - 这样您就可以告诉搜索引擎将旧URL替换为新URL,您不必担心关于复杂的多个.htaccess重写规则。

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

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