Beginner Url Rewrite htaccess

允我心安 提交于 2019-12-19 11:42:54

问题


I would simply like to rewrite all requests from:

http://example.com/products/product.cfm?id=product-name

to

http://example.com/products/product-name

and secondly,

http://example.com/category.cfm?id=some-category&sub=sub-category

to

http://example.com/some-category/sub-category

Here is what I’ve tried:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^products/$1 ^products/product.cfm?id=$1 [NC] 

I'm sure that makes no sense, as I really have no idea what I am doing. I was hoping someone could show me where I'm going wrong so I could follow by example.

Thanks! George


回答1:


http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/

I don't like pure "link" answers, but the link above is extremely helpful to me :]




回答2:


Using mod_rewrite, you could write:

RewriteRule ^products/([\w-]+)$ /products.cfm?id=$1&%{QUERY_STRING}

This will do the translation you wanted, while preserving other parameters in the original URL. It won't handle categories, but I'm not sure you can have those both in operation simultaneously as you described.




回答3:


Something along these lines:

RewriteCond %{query_string} &?id=([^&]+) [NC] 
RewriteRule ^/products/product.cfm$ /products%1? [R,NC,L]

# Fragile, this assumes that params will always be in order id= then sub=
RewriteCond %{query_string} &?id=([^&]+)&sub=([^&]+) [NC] 
RewriteRule ^/category.cfm?$ /%1/%2? [R,NC,L]

The R flag in the RewriteRule forces a hard redirect (the visitor's location bar will change to the new URL). Remove this flag for an internal redirect.



来源:https://stackoverflow.com/questions/2337537/beginner-url-rewrite-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!