htaccess缩短网址

时间:2010-11-18 05:13:40

标签: .htaccess url-rewriting

我想缩短

  

www.site.com/product/info/laptop to www.site.com/laptop

我用过

RewriteRule ^(.+)$ /product/info/$1

但我得到500内部服务器错误

当我尝试时,

RewriteRule ^([a-zA-Z0-9_\s\'~%,:!?()_=&-]+)$ /product/info/$1

它有效但我也想支持这段时间,所以当我加入时。

RewriteRule ^([a-zA-Z0-9_\s\'~%,:!?()\._=&-]+)$ /product/info/$1

它给了我500内部服务器错误

你能解释一下发生了什么吗?

谢谢

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

RewriteCond %{REQUEST_URI} ^/product/info/(.*)$
RewriteRule ^(.*)?$        /%2                  [R=301,L]

这将使用“Moved Permanently”标题将浏览器从www.example.com/product/info/laptop重定向到www.example.com/laptop。

如果您的意思是希望较短的网址在内部指向较长的网址,则必须避免循环重定向:

RewriteRule ^product/info/.*$ -                [L] # don't redirect again
RewriteRule ^(.*)$            /product/info/$1 [L] # redirect everything else

第一行将停止尝试将www.example.com/product/info/laptop重定向到www.example.com/product/info/product/info/laptop,等等。

修改

基于你的评论,看起来你也试图将除img,phpmyadmin等之外的所有内容重定向到索引?无论如何 - 你现在必须重新安排它,如下所示:

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico|product/info/(.*))
RewriteRule ^(.*)$ - [L] # don't redirect these

RewriteRule ^(.*)$ /product/info/$1 # redirect everything else

我不是第一次重写的“product / info /(.*)”部分的100%。如果这不起作用,请尝试:

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ - [L] # don't redirect these

RewriteRule ^product/info/.*$ -                [L] # don't redirect again

RewriteRule ^(.*)$ /product/info/$1 [L] # redirect everything else

修改2

根据您的评论做出最终答案:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ - [L] # don't redirect these

# pass controllers to index.php
RewriteCond %{REQUEST_URI} ^/(home|browse|calendar|star|member|enter|product)
RewriteRule ^(.*)$ /index.php?$1 [L]

# pass other things than controllers to /index.php?/product/info/$1
RewriteRule ^(.*)$ /index.php?/product/info/$1 [L] # redirect everything else