mod_write重写规则

发布于 2024-11-03 11:44:28 字数 861 浏览 1 评论 0原文

我确信这很简单,但我完全无法让它工作:\ 我正在尝试将

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 元素来解决这个问题,我的正则表达式一开始没有任何问题!

我把

在 HEAD 部分,现在一切都很好!

感谢那些向我展示如何设置天气条件或文件/目录不存在的人

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 技术交流群。

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

发布评论

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

评论(2

痴情换悲伤 2024-11-10 11:44:28

在您的 .htaccess 文件中尝试此规则:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([-a-z0-9]+)$ /index.php?page=shop&code=$1 [L,NC,QSA]

Try this rule in your .htaccess file:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([-a-z0-9]+)$ /index.php?page=shop&code=$1 [L,NC,QSA]
不醒的梦 2024-11-10 11:44:28
      RewriteEngine On
      RewriteBase /

      # Generic requests for the application
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^products/(.*)$ index.php?page=shop&code=$1 [L]

就这么办吗?

编辑:啊,好吧,您的页内链接错误是您应该在 PHP 中修复的问题。你可以尝试这个:

    RewriteEngine On
    RewriteBase /

    # Generic requests for the application
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Main page
    RewriteRule ^products/([A-Za-z0-9]+)$ index.php?page=shop&code=$1 [L]
    RewriteRule ^products/?(.*)$ index.php?$1 [L]

    # Resources
    RewriteRule ^products/images/(.*)$ images/$1 [L]
    RewriteRule ^products/css/(.*)$ css/$1 [L]
    RewriteRule ^products/js/(.*)$ js/$1 [L]

但实际上,你应该在 php 中生成你的 URL,这样它们才能正常工作:比每次都通过 mod_rewrite 运行它们更有效率。

      RewriteEngine On
      RewriteBase /

      # Generic requests for the application
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^products/(.*)$ index.php?page=shop&code=$1 [L]

That do it?

EDIT: Ah, well, your in-page links being wrong is something you should probably fix in PHP. You might try this:

    RewriteEngine On
    RewriteBase /

    # Generic requests for the application
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Main page
    RewriteRule ^products/([A-Za-z0-9]+)$ index.php?page=shop&code=$1 [L]
    RewriteRule ^products/?(.*)$ index.php?$1 [L]

    # Resources
    RewriteRule ^products/images/(.*)$ images/$1 [L]
    RewriteRule ^products/css/(.*)$ css/$1 [L]
    RewriteRule ^products/js/(.*)$ js/$1 [L]

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.

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