2013-02-10 44 views
0

我一定是个白痴,因为我无法工作。Mod重写删除网址的组成部分

我有一个网址: www.site.com.au/products/product-name.html

我需要将这些重定向到: www.site.com.au/product-name .html

所有链接都是动态的,该文件夹不存在。我用什么ReWrite规则来实现这个目标?

这是我到目前为止有:

RewriteCond %{HTTP_HOST} ^(www|test)\.site\.com\.au 
RewriteCond %{REQUEST_URI} ^(/products/) 
RewriteCond %{REQUEST_URI} [A-Z] 
RewriteRule ^.+\.html$ ${lc:%{REQUEST_URI}} [NC,R=301,L] 

只需要将位添加到远程/产品

感谢。

回答

0

好吧,我真的不知道Apache Rewrite是否知道确切的格式。但经过无事生非这些都是工作的结果:

# Lowercase all /products/ 
RewriteCond %{HTTP_HOST} ^(www)\.site\.com\.au 
RewriteCond %{REQUEST_URI} ^/products/.+\.html 
RewriteCond %{REQUEST_URI} [A-Z] 
RewriteRule^${lc:%{REQUEST_URI}} [R=301,L] 

# Lowercase all /products/ and strip products/ subfolder 
RewriteCond %{HTTP_HOST} ^(www)\.site2\.com\.au 
RewriteCond %{REQUEST_URI} ^/products/.+\.html 
RewriteCond %{REQUEST_URI} [A-Z] 
RewriteRule ^products/(.+\.html)$ /${lc:$1} [R=301,L] 

感谢,

大教堂

0
RewriteRule ^products(/.*)$ http://www.site.com.au$1 [L, R=301] 

这取代你上市,除第一的RewriteCond一切(相匹配的域名,但如果你的虚拟主机只在这两个领域的回答,您可以排除的RewriteCond简化它)。

在Apache查看RewriteConds之前,RewriteRules首先被匹配,所以如果你可以在RewriteRule本身中进行匹配,它将大大简化事情。只是以备将来参考,如果你确实需要在的RewriteCond来匹配,这将是这个样子:

RewriteCond %{REQUEST_URI} ^/products(/.*)$ 
RewriteRule ^.*$ http://www.site.com.au%1 [L, R=301] 

注意%1匹配什么是在括号中的RewriteCond主场迎战$1匹配什么在RewriteRule。

编辑:根据您的评论,以下修改应强制小写。我自己并不需要这样做,但通过this Apache documentation这是一个通过RewriteMap的内部函数。根据你的原始代码,看起来你可能已经在其他地方拥有了RewriteMap定义。如果不是的话,我把它包括在这里。

RewriteMap lc int:tolower 
RewriteCond %{REQUEST_URI} ^/products(/.*)$ [NC] 
RewriteRule ^.*$ http://www.site.com.au${lc:%1} [L, R=301] 
+0

谢谢,但似乎这并不包括液晶元件,但 - 我们需要小写整个URL ... – Dom 2013-02-10 10:26:57

+0

不是我以前做过的事情,但我更新了我认为会强制小写的答案。 – 2013-02-10 18:02:27