简体   繁体   English

正则表达式用于重写.htaccess中的旧URL

[英]Regex for Rewriting Old URL in .htaccess

I'm not sure what the rewrite rule in my .htaccess needs to look like. 我不确定.htaccess中的重写规则应该是什么样子。 My goal is to support old URL's that have parameters in them and convert them to the new URL. 我的目标是支持其中包含参数的旧URL,并将其转换为新URL。

Old URL 旧网址

http://site1.domain.com/product.php?ID=12357/$10-gift-certificate

New URL 新网址

http://site1.domain.com/item/12357/$10-gift-certificate

The URL has various variables in it: URL中包含各种变量:

  1. Subdomain (site1) 子域(site1)
  2. Item/Product ID (12357) 项目/产品编号(12357)
  3. Description ($10-gift-certificate) 说明($ 10礼品证书)

So put another way, the architecture of the updated url looks like this: 换句话说,更新后的URL的架构如下所示:

http://[subdomain].domain.com/item/[ID]/[description]

Thanks for any suggestions! 感谢您的任何建议!

Try this rule : 试试这个规则:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC]
RewriteCond %{THE_REQUEST} /product\.php\?id=([^/]+)/([^\s]+) [NC]
RewriteRule ^ item/%1/%2? [NE,NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^item/([^/]+)/([^/]+)/?$ product.php?id=$1/$2 [QSA,NC,L]

Instead of supporting the old one you really are supporting the new ones like this 而不是支持旧版本,您实际上是在支持新版本

 RewriteRule  /?category/([^/d]+)/?$ index.php?category=$1 [L,QSA]

Where your new url would be ( what the user sees ) 您的新网址将在哪里(用户看到的内容)

yoursite.com/category/1

Which would map to ( what the server sees ) 哪个将映射到(服务器看到的内容)

index.php?category=$1

Old urls would work fine, as such. 这样,旧的网址就可以正常工作。 The user ( end client ) would see only the url you have links for on your webisite ( the new format I assume ), And because these are not redirects the url will not change in the browser. 用户(最终客户)只会在您的网站上看到您具有链接的URL(我假定的新格式),并且由于这些不是重定向,因此该URL在浏览器中不会更改。

So in your example 所以在你的例子中

 http://[subdomain].domain.com/item/[ID]/[description]

You'd want something like this 你想要这样的东西

RewriteRule  /?item/([\d]+)/([^/]+)/?$ product.php?ID=$1&description=$2 [L,QSA]

So this would be 所以这将是

INPUT INPUT

 http://site1.domain.com/item/12357/$10-gift-certificate

OUTPUT OUTPUT

 http://site1.domain.com/product.php?ID=12357&description=$10-gift-certificate 

-note- that the browser will not [R] redirect so the url will not change to output only the server sees it this way. -请注意,浏览器不会[R]重定向,因此url不会更改为仅服务器以这种方式看到的输出。

unless ID=12357/$10-gift-certificate is really what you mean for the old url and I don't know what [subdomain] maps to. 除非ID=12357/$10-gift-certificate确实是您对旧网址的意思,并且我不知道[subdomain]映射到什么。 Which in that case you would want this. 在那种情况下,您想要这个。

 RewriteRule /?item/(.+?)/?$ product.php?ID=$1 

Which given the same input above would output 上面给出相同输入的哪个将输出

 http://site1.domain.com/product.php?ID=12357/$10-gift-certificate 

So you see that the client sees the new url, but the server sees the old one. 因此,您看到客户端看到了新的URL,而服务器看到了旧的URL。 Which is why I said most people look at this backwards, in the beginning. 这就是为什么我说大多数人从一开始就把这个问题向后看的原因。

Anyway, here is a site to test url rules on 无论如何,这是一个测试网址规则的网站

http://htaccess.madewithlove.be/ http://htaccess.madewithlove.be/

I am sure there are other sites out there too. 我敢肯定还有其他网站。

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

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