mod_write重写规则
我确信这很简单,但我完全无法让它工作:\ 我正在尝试将
http://www.example.com/products/ABC123
重写为
http://www.example.com/index.php?page=shop&code= ABC123
我已经尝试过
RewriteBase /
RewriteRule ^products/([A-Za-z0-9]+)$ index.php?page=shop&code=$1
哪个重定向正常,但所有图像(和 js css 等)都有错误的路径(example.com/images/ABC123.jpg 是 example.com/products/images/ABC123.jpg)。此外,所有链接现在都是错误的路径(example.com/?page=shop&folder=7 是 example.com/products/?page=shop&folder=7)
我的 .htaccess 文件位于根目录中,我有还尝试了 RewriteBase products/ 并从规则正则表达式中删除 products/ 但这只是抛出 404
我在过去一个小时内多次阅读了有关 mod_rewrite 的官方文档,我一定错过了一些东西?
** 编辑: ** 抱歉,这都是一个有点棘手的问题,事实证明你可以使用 html BASE 元素来解决这个问题,我的正则表达式一开始没有任何问题!
我把
感谢那些向我展示如何设置天气条件或文件/目录不存在的人
im sure this is simple but i totally cant get it to work :\
I'm trying to rewrite
http://www.example.com/products/ABC123
to
http://www.example.com/index.php?page=shop&code=ABC123
I have tried
RewriteBase /
RewriteRule ^products/([A-Za-z0-9]+)$ index.php?page=shop&code=$1
Which redirects OK but all the images (and js css etc) have the wrong path (example.com/images/ABC123.jpg is example.com/products/images/ABC123.jpg). Also all of the links are now the wrong path (example.com/?page=shop&folder=7 is example.com/products/?page=shop&folder=7)
My .htaccess file is in the root, I have also tried RewriteBase products/ and removing products/ from the rules regex but that just throws a 404
I've read the official docs concerning mod_rewrite several times in the last hour, I must be missing something?
** EDIT: **
Sorry all was a bit of a trick question, turns out you can solve this with the html BASE element, there was nothing wrong with my regex at the start!
I put <base href="http://www.example.com/" /> in the HEAD section and it's all fine and dandy now!
Thanks to the guys who showed me how to set conditions for weather or not the file/directory exists
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的 .htaccess 文件中尝试此规则:
Try this rule in your .htaccess file:
就这么办吗?
编辑:啊,好吧,您的页内链接错误是您应该在 PHP 中修复的问题。你可以尝试这个:
但实际上,你应该在 php 中生成你的 URL,这样它们才能正常工作:比每次都通过 mod_rewrite 运行它们更有效率。
That do it?
EDIT: Ah, well, your in-page links being wrong is something you should probably fix in PHP. You might try this:
But really, you should generate your URLs in php so they work right: would be more efficient than having to run them through mod_rewrite every time.