初学者 URL 重写 htaccess

发布于 2024-08-22 19:51:39 字数 588 浏览 2 评论 0原文

我只想重写所有请求:

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

to

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

和 其次,

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

to

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

这是我尝试过的:

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

我确信这没有意义,因为我真的不知道我在做什么。我希望有人能告诉我哪里出错了,这样我就可以以身作则。

谢谢! 乔治

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情仇皆在手 2024-08-29 19:51:40

使用 mod_rewrite,您可以这样写:

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

这将进行您想要的翻译,同时保留原始 URL 中的其他参数。它不会处理类别,但我不确定您是否可以按照您的描述同时操作这两个类别。

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.

波浪屿的海角声 2024-08-29 19:51:40

大致思路如下:

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]

RewriteRule 中的 R 标志强制进行硬重定向(访问者的位置栏将更改为新的 URL)。删除此标志以进行内部重定向。

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.

烛影斜 2024-08-29 19:51:39

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

我不喜欢纯粹的“链接”答案,但上面的链接对我非常有帮助:]

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 :]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文